Jump to content

  • Log In with Google      Sign In   
  • Create Account

Simple bit masking operations


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 musafir2007   Members   -  Reputation: 191

Like
1Likes
Like

Posted 19 October 2011 - 10:28 AM

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

Sponsor:

#2 Serapth   Crossbones+   -  Reputation: 5755

Like
1Likes
Like

Posted 19 October 2011 - 11:15 AM

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

#3 fastcall22   Crossbones+   -  Reputation: 4456

Like
0Likes
Like

Posted 19 October 2011 - 11:25 AM

Now for example I have a variable n, and I want to set its 10th bit to 1, how do I do that?


n |= 1 << 9; // set
n ^= 1 << 9; // toggle
n = n & ~(1 << 9); // clear


EDIT:
Serapth's link's good too. :D
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#4 Serapth   Crossbones+   -  Reputation: 5755

Like
0Likes
Like

Posted 19 October 2011 - 11:37 AM

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.

#5 musafir2007   Members   -  Reputation: 191

Like
0Likes
Like

Posted 19 October 2011 - 02:52 PM

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

#6 BeerNutts   Crossbones+   -  Reputation: 2999

Like
0Likes
Like

Posted 20 October 2011 - 10:12 AM

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)

#7 musafir2007   Members   -  Reputation: 191

Like
0Likes
Like

Posted 26 October 2011 - 01:18 PM

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 <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/sad.gif' class='bbc_emoticon' alt=':(' />
   }
}

What am I doing wrong here and how can I fix it? thanks!

#8 ApochPiQ   Moderators   -  Reputation: 16397

Like
1Likes
Like

Posted 26 October 2011 - 01:25 PM

tMask & tIndex is not the same thing as checking if the tIndex-th bit on tMask is set ;-)

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS