Skip to main content
gamedev.net
🔒 Locked

Access individual bits?

Started by bzroom 2 replies 500+ views
Original Post
bzroom
bzroom
I''m making a driving game and to send the status to the server i would like to use only a single byte. How do i access each bit turning 1 byte into 8 booleans? I could do it where every possible combination had a value between 0-256 but no thanks, there has to be a better way.
Dave Hunt
Dave Hunt
Using bit logic:

byte AND 1 = bit 0
byte AND 2 = bit 1
byte AND 4 = bit 2
byte AND 8 = bit 3
byte AND 16 = bit 4
byte AND 32 = bit 5
byte AND 64 = bit 6
byte AND 128 = bit 7

So, if you want to check if bit 4 is set, you would use something like:

if (byte AND 16) != 0 then do something

As a C/C++ example:

if (byte & 16)
{
do something
}
bzroom
bzroom
Thanx.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.