&-operator?
Disecting Joseph Farell''s Game Programming Genesis: Basic Tile Engines. Excellent article, but I run into some problem with some of the code.
// check if the camera coordinates are divisible by 32
x = mapdata.xCamera & 0x0000001F;
Obviously it''s a check to determine whether the camera coordinates are divisable by 32. :D How does it actually work? Not familiar with the &-operator. Someone please give me a explanation. Thanks!
& in C and C++ is the bitwise and operator. Basically, it compares each bit in the two arguments and if the corresponding bit of each is 1, the resulting bit is 1. Like this. (the underscore is to make the columns line up)
_ 11100101011
& 01101010011
= 01100000011
You see? Since 0x0000001F = 31 = 000...00011111, doing a binary and with that returns all 0's execept for the last 5 bits, where it will return whatever the last 5 bits of mapdata.xCamera were.
If you are taking modulus with a number that is a power of 2, you can use binary & instead, which is faster. In other words,
x % 32 == x & 31
x % 64 == x & 63
etc.
If the result is 0, the number is divisible by 32. & can also be used to grab bits from a number- in other words x & 8 grabs the fourth bit. (it would still be in the fourth place in the number).
Edited by - FallingFrog on February 13, 2001 4:35:20 PM
_ 11100101011
& 01101010011
= 01100000011
You see? Since 0x0000001F = 31 = 000...00011111, doing a binary and with that returns all 0's execept for the last 5 bits, where it will return whatever the last 5 bits of mapdata.xCamera were.
If you are taking modulus with a number that is a power of 2, you can use binary & instead, which is faster. In other words,
x % 32 == x & 31
x % 64 == x & 63
etc.
If the result is 0, the number is divisible by 32. & can also be used to grab bits from a number- in other words x & 8 grabs the fourth bit. (it would still be in the fourth place in the number).
Edited by - FallingFrog on February 13, 2001 4:35:20 PM
"The reasonable man adapts himself to the world, butthe unreasonable man tries to adapt the world to him-therefore, all progress depends on the unreasonable man." -Samuel Butler
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement