WALL1 -> 128 -> 10000000
WALL2 -> 129 -> 10000001
WALL3 -> 130 -> 10000010
WALL4 -> 131 -> 10000011
and so on. In fact, that first bit will be unchanged until it gets to 256 (which would be 00000001 00000000).
This statement:
foo & 128
takes the value of foo and does a bitwise AND with the constant value 128.
The AND operation takes two inputs and produces one output. The result table is like this:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
So, only when both inputs are true is the output true. A bitwise operation looks at each bit of the inputs one at a time, and produces the output one bit at a time. So, if foo has a value of, say, 131 (a WALL4), the operation looks like this:
131 10000011& 128 10000000----------------result 10000000
The result is what it is because only in the first bit is there a true value in both inputs. Because this result is a non-zero value, it is considered to be a true result for the purposes of an if statement. So effectively, by saying:
if( foo & 128 )
you are simply checking to see if the eighth bit is set in the value of foo. Any value between 128 and 255 will have that bit set, so unless you have more than 127 different types of WALL, that should do the job.
Does that make any sense?
[edited by - Plasmadog on March 24, 2004 10:05:59 PM]