Simple bit masking operations

Started by
6 comments, last by ApochPiQ 12 years, 5 months ago
Hi all,
So i am a little weak in all this bits stuff..

So far I realized that if I want to see if a particular bit is set...
for variable n, to see if kth bit is set, I will
unsigned n = ...
if ((1 << k) & n)
bit set...
else
not set...

Now for example I have a variable n, and I want to set its 10th bit to 1, how do I do that? Will I need to check first, or can I just force it to be 1 in a single expression?

thanks
Advertisement
These methods count bits from most significant bits to least significant bits.
The set methods use pointers because they have side effects. Just place & in front of the variable to give it's address as a pointer.
The PowerOfTwo array only exist because all methods for integer exponents in C++ are ugly hacks and we only need 16 values.

const UINT16 PowerOfTwo[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
bool GetBitFromUINT8(unsigned char X, int Index) {
if (Index < 0 || Index > 7) {
printf("GetBitFromUINT8: Index is out of bound.");
return false;
}
return (X & PowerOfTwo[7 - Index]) > 0;
}

bool GetBitFromUINT16(UINT16 X, int Index) {
if (Index < 0 || Index > 15) {
printf("GetBitFromUINT16: Index is out of bound.");
return false;
}
return (X & PowerOfTwo[15 - Index]) > 0;
}

void SetBitInUINT8(unsigned char* pX, int Index, bool NewValue) {
if (Index < 0 || Index > 7) {
printf("SetBitInUINT8: Index is out of bound.");
} else {
if (NewValue) {
*pX = *pX | PowerOfTwo[7 - Index];
} else {
*pX = *pX & ~(PowerOfTwo[7 - Index]);
}
}
}

void SetBitInUINT16(UINT16* pX, int Index, bool NewValue) {
if (Index < 0 || Index > 15) {
printf("SetBitInUINT16: Index is out of bound.");
} else {
if (NewValue) {
*pX = *pX | PowerOfTwo[15 - Index];
} else {
*pX = *pX & ~(PowerOfTwo[15 - Index]);
}
}
}
This write up probably explains it about as well as I ever could, and it takes me a hell of a lot less time to simply post a link. :)

Now for example I have a variable n, and I want to set its 10th bit to 1, how do I do that?
[font="Courier New"]
n |= 1 << 9; // set
n ^= 1 << 9; // toggle
n = n & ~(1 << 9); // clear
[/font]

EDIT:
Serapth's link's good too. :D
Oh, and if this is for actual production code and not as a learning exercise, you should probably use std::bitset.



long bitValues = 0x3B9ACA00; // 1000000000 in hex
std::bitset<10> myBits(bitValues);
bool isTenthBitSet = myBits.test(9);


or


std::bitset<10> myBits("1000000000",0,9);
bool isTenthBitSet = myBits.test(9);





Not, I assumed everything was 0 based, but I would verify.
Thanks for your replies. So basically I have..

func( params...)
{
useScaling = n & 1<<10;

if(useScaling)
{
//scaling algorithm
}
}

So now if I do want to do scaling..
would I do

n |= 1 << 9; // set
or
n |= 1 << 10; // set

func(n);

?

Thanks

Thanks for your replies. So basically I have..

func( params...)
{
useScaling = n & 1<<10;

if(useScaling)
{
//scaling algorithm
}
}

So now if I do want to do scaling..
would I do

n |= 1 << 9; // set
or
n |= 1 << 10; // set

func(n);

?

Thanks


Realize, when you say the "10th" bit, since bit 0 is "1st" bit, then bit 9 would be the "10th". But, it doesn't really matter as long as you use the same shift operator to set and to get.

Also, Be careful with operator precedence. If you're not sure what gets executed 1st, you should use (). Since I'm not 100% sure, I would change the line to be this:

useScaling = n & (1<<10);


Could turn out to be useScaling = (n & 1) << 10 (even though it isn't, it's good practice to use parenthesis for multiple operators)

So, to Set a bit for scaling (and you are using the 11th bit, which is bit 10), and check a bit, you'd do this:

// set for scaling
BitFlag |= (1 << 10);

// Check if we're using scaling
useScaling = BitFlag & (1 << 10);

if (useScaling)...

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Hello, I am still a little bit confused. Here's my issue



func( int x, int y, unsigned char tIndex )
{
unsigned char tMask = 0;

if (x > 0 && y > 0)
{
tMask |= (1<<tIndex); //set a bit
}
else
{
tMask &= ~(1<<tIndex);
}


if ( tMask & tIndex)
{
//should be here if x and y were greater than 0 right???
}
else
{
//but comes here :(
}
}


What am I doing wrong here and how can I fix it? thanks!
tMask & tIndex is not the same thing as checking if the tIndex-th bit on tMask is set ;-)

You probably meant tMask & (1 << tIndex).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement