What does this FOR loop means?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zenoba
    New Member
    • Oct 2008
    • 8

    What does this FOR loop means?

    Can someone tell me what does this FOR loop means in C programming?
    for (;a!=0;a=a>>1)

    what this for loop does?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The >> operator is the 'right shift' operator and it shifts its left operand's bits to
    the right by 'n' bits where 'n' is the value of its right operand. So a= a>>1 shifts
    the bits of the value of a to the right one bit.

    The loop condition tests whether or not a equals zero; if it isn't there's at least
    one 1 bit left in the value of a so the loop continues to shift right the value one
    more bit and over and over again until all the 1 bits are shifted out of a to the
    right.

    kind regards,

    Jos

    Comment

    • vinayvaka
      New Member
      • Nov 2007
      • 13

      #3
      for (;a!=0;a=a>>1)
      st1;


      first compiler check the value of a; if it is not equal to zero it will proced after that st1 will execute then a will be right shift(>> is a right shift operator) once i.e lets take a value is 8, then binary is 1000 after right shift it will be 0100, so compiler will check the a value again now its value is 4, it will repeate till a value zero.

      Comment

      • Tassos Souris
        New Member
        • Aug 2008
        • 152

        #4
        a >> 1 is equivalent to a / 2

        In general, a >> b is equivalent to a / 2 ^ b.

        This of course if a is integer...

        Comment

        Working...