Sep 29, 2008 at 4:00pm UTC
Hi volks,
could anyone tell me how do these functions exactly work?
void setmax()
{
unsigned l=-1;
if(!max)
while (max++, l >>=1)
;
}
and
void printbyte()
int i;
while (i--) printf("%c", byte_ & 1U<<i ? '1':'0');
puts("");
}
I could not understand the bolded commands :(
Is there any other alternatives how to write this code in a better (easier) way?
Thank you in avance!
Dan
Last edited on Sep 29, 2008 at 5:15pm UTC
Sep 29, 2008 at 4:05pm UTC
those are while loops. They keep going until whatever is in parenthesis evaluates to false (zero).
For instance in the second loop, it decrements i by 1 every time and prints the byte. Thus this loop will terminate when i==0 if i is initially 100, then the loop will go 100 times and then move on to the next piece of code.
Sep 29, 2008 at 4:35pm UTC
Thanks!!! But what I really did not understand is what do these commands in the above listed examples do? what do they mean?
l >>=1 ?
and
byte & 1U <<i ?
Thanks!!
Last edited on Sep 29, 2008 at 5:57pm UTC
Sep 29, 2008 at 6:26pm UTC
l >>= 1;
is equivalent to l = l >> 1;
which
is a bit shift right 1 bit (ie, equivalent to integer division by 2)
byte & 1U << i
is shifting the integer value 1 left i
bits and then bitwise ANDing the result with byte .
And the comma in the first while loop is evil.