Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:30:52+00:00 2026-05-31T09:30:52+00:00

Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit

  • 0

Possible Duplicate:
Best algorithm to count the number of set bits in a 32-bit integer?

How do I count the number of 1‘s a number will have in binary?

So let’s say I have the number 45, which is equal to 101101 in binary and has 4 1‘s in it. What’s the most efficient way to write an algorithm to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:30:53+00:00Added an answer on May 31, 2026 at 9:30 am

    Instead of writing an algorithm to do this its best to use the built in function. Integer.bitCount()

    What makes this especially efficient is that the JVM can treat this as an intrinsic. i.e. recognise and replace the whole thing with a single machine code instruction on a platform which supports it e.g. Intel/AMD


    To demonstrate how effective this optimisation is

    public static void main(String... args) {
        perfTestIntrinsic();
    
        perfTestACopy();
    }
    
    private static void perfTestIntrinsic() {
        long start = System.nanoTime();
        long countBits = 0;
        for (int i = 0; i < Integer.MAX_VALUE; i++)
            countBits += Integer.bitCount(i);
        long time = System.nanoTime() - start;
        System.out.printf("Intrinsic: Each bit count took %.1f ns, countBits=%d%n", (double) time / Integer.MAX_VALUE, countBits);
    }
    
    private static void perfTestACopy() {
        long start2 = System.nanoTime();
        long countBits2 = 0;
        for (int i = 0; i < Integer.MAX_VALUE; i++)
            countBits2 += myBitCount(i);
        long time2 = System.nanoTime() - start2;
        System.out.printf("Copy of same code: Each bit count took %.1f ns, countBits=%d%n", (double) time2 / Integer.MAX_VALUE, countBits2);
    }
    
    // Copied from Integer.bitCount()
    public static int myBitCount(int i) {
        // HD, Figure 5-2
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }
    

    prints

    Intrinsic: Each bit count took 0.4 ns, countBits=33285996513
    Copy of same code: Each bit count took 2.4 ns, countBits=33285996513
    

    Each bit count using the intrinsic version and loop takes just 0.4 nano-second on average. Using a copy of the same code takes 6x longer (gets the same result)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best way to detect integer overflow in C/C++ If I have an
Possible Duplicate: Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? What constitutes a
Possible Duplicate: Best practices in error reporting (Mathematica) Assume that I have a function
Possible Duplicate: Best way to detect integer overflow in C/C++ how do we check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.