Jump to content
  • Advertisement

Archived

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

En3my

&-operator?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
& 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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!