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

  • Home
  • SEARCH
  • 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 7446559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:20:38+00:00 2026-05-29T12:20:38+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?

Given a 32bit unsigned integer, we want to count the number of non-zero bits in its binary representation. What is the fastest way to do that ?

We want to do this N~10^10 times.

note: using a large look up table is usually not a good idea because of the architecture of current cpu’s . it is much faster to calculate it locally than to use a huge array that needs looking at the external memory

  • 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-29T12:20:39+00:00Added an answer on May 29, 2026 at 12:20 pm

    There are actually several options, I presume the native way is way too slow for this.

    You can go with lookup table for 8-bit value and do it in parallel for all four bytes from unsigned int value, then sum the result. This one could be also quite well-paralelizable (be it multi-core, or maybe even some SSE3/4 could help).

    You can also go with Brian Kernighan’s solution:

    unsigned int v;              // count the number of bits set in v
    unsigned int c;              // c accumulates the total bits set in v
    for (c = 0; v; c++)
    {
      v &= v - 1;                // clear the least significant bit set
    }
    

    And the last possible way I found somewhere some time ago is (on 64-bit machines, as the modulo operation would be really fast there):

    unsigned int v;      // count the number of bits set in v
    unsigned int c;      // c accumulates the total bits set in v
    
    c =  ((v & 0xfff) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
    c += (((v & 0xfff000) >> 12) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
    c += ((v >> 24) * 0x1001001001001ULL & 0x84210842108421ULL) % 0x1f;
    
    • 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 for Bit Reversal ( from MSB->LSB to LSB->MSB) in C
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? What constitutes a
Possible Duplicate: Best algorithm to test if a linked list has a cycle p=head;
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? I need to
Possible Duplicate: Best way to detect integer overflow in C/C++ If I have an
Possible Duplicate: Best way to iterate through a directory in java? I want to
Possible Duplicate: Best way to detect integer overflow in C/C++ What is the best

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.