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   Members   

201
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

#2   Members   

6652
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   Moderators   

9751
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
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#4   Members   

6652
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   Members   

201
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   Members   

4288
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   Members   

201
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   Moderators   

21412
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).
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]




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.