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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:36:46+00:00 2026-05-17T16:36:46+00:00

I was trying to learn more about bits, and I came across this example

  • 0

I was trying to learn more about bits, and I came across this example.

How does this code work to count the bits? (My C is very rusty, by the way).

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; v >>= 1)
{
  c += v & 1;
}
  • 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-17T16:36:47+00:00Added an answer on May 17, 2026 at 4:36 pm

    v & 1 is 1 if the lowest bit in v is set, and 0 if it’s not.

    The loop keeps dividing shifting the bits of vright by 1 place each iteration, and thus the lowest bit of v loops over each of the bits in the original v (as each one gets to the 1’s place before “falling off” the fractional end).

    Thus, it loops over each bit and adds 1 to c if it’s set, and adds 0 if it’s not, thus totaling the set bits in the original v.

    For example, with a starting v of 1011:

    1. 1011 & 1 = 1, so c is incremented to 1.
    2. Then 1011 is shifted to become 101.
    3. 101 & 1 = 1, so c is incremented to 2.
    4. 101 is shifted to become 10.
    5. 10 & 1 = 0, so c isn’t incremented and remains 2.
    6. 10 is shifted to become 1.
    7. 1 & 1 = 1, so c is incremented to 3.
    8. 1 is shifted to become 0 (because the last bit fell off the fractional end).
    9. Since the condition of the for loop is just v, and v is now 0, which is a false value, the loop halts.

    End result, c = 3, as we desire.

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

Sidebar

Related Questions

Possible Duplicate: How does a debugger work? I'm trying to learn more about debuggers.
So I'm trying to learn more about lambda expressions. I read this question on
I'm trying to learn more about how systems really work underneath all the pretty
I am trying to learn more about java. This program is an attempt to
I'm trying to learn more about how web and tcp work by implementing web
I'm trying learn more about RavenDB and at the moment to focus on how
I'm trying to learn more about the javascript libraries available and what each might
I am trying to learn more about the DOM and have been writing some
I'm trying to learn more about the Dispose method and how things happening in
I'm trying to learn more about WPF. I ran through an online tutorial that

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.