#### Archived

This topic is now archived and is closed to further replies.

# &-operator?

This topic is 6482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
& 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

##### Share on other sites
Thanks for taking the time giving me a great explanation! It''s all clear to me now!

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011159
• ### Who's Online (See full list)

There are no registered users currently online

×