• 12
• 12
• 9
• 10
• 13

# Modifying single bits of a variable (C++)

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

## Recommended Posts

Hi, Recently, I've found using the single bits of say, a long integer, is very useful for storing true/false, on/off, etc... information. For example, in the game I am making at the moment, the player has various 'states', such as moving, shooting, crouching, etc... I know that I can say: STATE_MOVE = 1; STATE_SHOOT = 2; STATE_CROUCH = 4; STATE_... = 8; etc..., and have them in powers of 2. Then you can use |= to set the different bits, then (I think) you can use ^= (XOR) to turn off the different bits. To check whether a bit is on you can use & comparitively. I was wondering though, is there a way you can say "set the nth bit of this variable to 0/1", and "is the nth bit of this variable 0/1?", without having to work in powers of 2. It isn't really a necessity for my project, but I'm just curious as to whether this is possible. Thanks, Harry

##### Share on other sites
You can use the bitshift operator to do what you are asking, something like this:
int bit = 1 << bitNumber;

Replace bitNumber with the number of the bit you would like to access, and then do the same thing you have been doing to set or clear the bit. Hope that's what you were asking for.

##### Share on other sites
Yep, that's perfect.

##### Share on other sites
If you're using C++ it might also be a good idea to check out bitset, which makes it much easier to manipulate bits.

##### Share on other sites
Take a look at structs aswell, you can assign bitfields to structure member too.

struct PlayerState{  unsigned move:1;  unsigned shoot:1;  unsigned crouch:1;};// You can use like thisstruct PlayerState PS;PS.move = true;PS.shoot = false;if (PS.move && !PS.crouch){  moveSpeed = 5.0;}else{  moveSpeed = 2.0;}

##### Share on other sites
If your doing this on a pc, its pretty worthless btw. You really only need to do this if your really low for memory. I mean it only saves at most 8 bytes.

##### Share on other sites
Quote:
 Original post by dpadam450If your doing this on a pc, its pretty worthless btw. You really only need to do this if your really low for memory. I mean it only saves at most 8 bytes.

I have a book at home, I'm not too sure of the exact title (I can look it up when I get home and post the title) but it is very similar to the game programming gem series and they recommend bitfields for the exact reason of game states because you could in theory have lots of states and all this memory adds up so saving at most 8 bytes isn't correct at all.

Another thing, if your sending packets down a network then bandwidth is very important and you want to stuff as much data as possible using the least amount of space.

##### Share on other sites
Quote:
 Original post by HarryuThen you can use |= to set the different bits, then (I think) you can use ^= (XOR) to turn off the different bits.

Just a note: variable |= 0x2; will set the second bit, as you say. XOR, however, is used to toggle bits. For example: variable ^= 0x2 will change the second bit from a 1 to a 0, and a 0 to a 1. To turn bits on and off, you want to AND them with a 0. So, to turn off the second bit of a 32 bit bitfield, variable &= 0xFFFFFFFFD;.

To avoid such trickiness, look into a library such as bitset, mentioned above. Boost probably has some good things, too.

[Edited by - jouley on April 22, 2007 8:12:09 PM]

##### Share on other sites
var |= 0x02 will set the second bit won't it.