tags:

views:

65

answers:

3

I have come across a few lines of coding I do not understand, and would be grateful for clarification:

  1. if(!(counter&7))
  2. ds->direction = ts->direction;

Thank you

+2  A: 

The first checks if the result of a bitwise-AND on the counter with 7 is not zero, and the latter assigns the value of the direction member of one struct to the direction member of another.

Chuck
Where "one struct" == "the struct pointed to by the pointer ds" and "another" == "the struct pointed to by the pointer ts".
sepp2k
+3  A: 
  1. if counter is a multiple of 8

  2. set the direction element of *ds equal to the direction element of *ts

Paul R
+1  A: 

1) same as

  1. if (!(counter & 7))
  2. if ((!(counter & 7)) != 0)
  3. if ((counter & 7) == 0)
  4. if the lower 3 bits of counter are zero (or if counter is a multiple of 8)

2) same as

  1. (*ds).direction = (*ts).direction;
  2. set direction of ds (must be of a struct type) to direction of ts
pmg
In (1), I think the parenthesized part of step 4 is only correct if `counter` is unsigned...
R..
It depends, I think, on representation. For sign and magnitude and two's complement, I think it holds (the three low bits being zero implies the number is a multiple of 8)
pmg
Indeed. Actually I'm curious what the standard says about signed bitwise operators. I couldn't find any language specific to signed or unsigned types so I assume they're required to operate bitwise on the representations.
R..