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 7508689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:43:06+00:00 2026-05-29T22:43:06+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?
Counting the number of bits that are set

How to find the number of bits set in an unsigned integer when it is allowed to iterate the loop same number of times as the number of bits set (if 3 bits are set, you can iterate the loop only 3 times)

  • 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-29T22:43:07+00:00Added an answer on May 29, 2026 at 10:43 pm

    This is a method I know of for doing this, it will only iterate for the number of bits set in v, at the end of execution c will hold the number of bits in v:

    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
    }
    

    source: C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)

    example:

    v = 1010; //2 bits are set
    v - 1 = 1010(10 decimal) - 1 = 1001(9 decimal)
    
    1010
    1001 &
    ------
    1000         The least significant bit is unset, c is incremented by 1
    
    v = 1000 
    v - 1 = 1000 - 1 = 0111
    
    1000
    0111 &
    -------
    0000         The least significant bit is unset again, c is incremented 
                 by 1, the loop stops because there are no more set bits.
    
    • 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 practices in error reporting (Mathematica) Assume that I have a function
Possible Duplicate: Best Java obfuscator ? For my example I know that eclipse offers
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

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.