Thread: Tricky for loop

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Tricky for loop

    I found a piece of code a while ago with a for loop that I don't understand. The purpose is counting the number of bits.

    Code:
    #include <stdio.h>
    
    int main()
    {
      unsigned int i;
      unsigned int v = 14;
    
      for (i=0; v; v>>=1) {
        i+= v & 1;
      }
    
      printf("# bits %d\n",i);
    
      return 0;
    }

    The question I have is what is happening here?
    Especially the code in the "for" statement, (some shifting):

    Code:
    v>>=1
    and later on (ANDing one bit or whole vector?):
    Code:
    i+= v & 1;

    I have no problem understanding bits and bytes. The result here is of course 3 bits.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps print the value in v each time around the loop (in binary for extra points), and also print it when the loop exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    I have of course done this. However I still have problem understanding the "v>>=1".

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hzcodec
    However I still have problem understanding the "v>>=1".
    Read up on bit shifting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    Well ... "v>>1" is shifting one step but what is the "=" sign doing in the for loop?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hzcodec
    Well ... "v>>1" is shifting one step but what is the "=" sign doing in the for loop?
    You may be familiar with syntax like:
    Code:
    x += 2;
    as shorthand for:
    Code:
    x = x + 2;
    The same idea applies here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    Oh yes! I'm familiar with the shorthands. And with a little a bit of experimenting with the code I finally got it. Just confusing myself. laserlight got me thinking in the right direction. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tricky C
    By ronrardin in forum C Programming
    Replies: 2
    Last Post: 09-29-2010, 01:01 PM
  2. explain me this tricky qs
    By ppanda04 in forum C Programming
    Replies: 9
    Last Post: 09-24-2010, 08:19 AM
  3. Tricky C Question
    By ganesh bala in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 12:58 PM
  4. tricky stuff...
    By dug in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2005, 08:23 AM
  5. very simple but tricky... please help
    By surdy in forum C Programming
    Replies: 6
    Last Post: 10-05-2004, 12:03 PM