Tricky for loop

This is a discussion on Tricky for loop within the C Programming forums, part of the General Programming Boards category; I found a piece of code a while ago with a for loop that I don't understand. The purpose is ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    16
    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
    21,414
    Quote Originally Posted by hzcodec
    However I still have problem understanding the "v>>=1".
    Read up on bit shifting.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    16
    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
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    16
    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, 11:58 AM
  4. tricky stuff...
    By dug in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2005, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21