gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > just need some clarification on my IF statement

#19800 - slboytoy - Sun Apr 25, 2004 8:51 pm

i just wanted to make sure that my if statement is properly set up. here's the code:
Code:
if(channels.hi & (1 << count - 31))



pretty much i'm wanting to cycle through the variable "channels.hi" one bit at a time to see if it is a high or a low and react accordingly.
does this statement look like it would check the bits in channels.hi???

#19802 - Miked0801 - Sun Apr 25, 2004 9:20 pm

Starting at the high bit right? Also, I'd put parenthesis around the shift operator as you can easily get burned by that as I'm not sure of order of precedence between the subtract and shift.

This is one that codes much easier in asm. Not the first time I want access to the carry flag from C... :)

#19803 - slboytoy - Sun Apr 25, 2004 9:22 pm

thanks!

#19804 - sajiimori - Sun Apr 25, 2004 9:46 pm

Shift is higher precedence than addition and subtraction (Edit: WRONG!! LOL -- just goes to show that parentheses are best). But anyway, just consider: if 'count' is 32, which bit of which word do you want? That should suggest the correct math.