Jump to content
  • Advertisement
Sign in to follow this  
Harryu

Modifying single bits of a variable (C++)

This topic is 4256 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

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


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


Link to post
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 this
struct PlayerState PS;

PS.move = true;
PS.shoot = false;

if (PS.move && !PS.crouch)
{
moveSpeed = 5.0;
}
else
{
moveSpeed = 2.0;
}

Share this post


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


Link to post
Share on other sites
Quote:
Original post by dpadam450
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.


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


Link to post
Share on other sites
Quote:
Original post by Harryu
Then 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 this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
var |= 0x02 will set the second bit won't it.

I knew I'd foul that up if I didn't check it. Just another reason not to roll your own! [Fixed, and thanks for the catch!]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!