Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

Simple bit masking operations

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

Share this post


Link to post
Share on other sites
Advertisement
Hidden
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]);
}
}
}

Share this post


Link to post

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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)...

Share this post


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

Share this post


Link to post
Share on other sites
tMask & tIndex is not the same thing as checking if the tIndex-th bit on tMask is set ;-)

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

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!