Jump to content
  • Advertisement
Sign in to follow this  

Working with binary

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

I want to work with binarys, i know the bitwise operators, but how do i check the first or last bit of a variable. I know about itoa and atoi, but do i need to convert to strings all the time? Anyone knows a tutorial about this? Thanks in advance, cya.

Share this post

Link to post
Share on other sites
To work with binary, like i will make an integer and use every bit as an flag, or antoher example.. in the game othello, i will use a 64 bit variable to store the positions of white pieces and another to store the positions of black pieces, so if i want to compare two boards, i will add the two 64 bit words of each board and subtract them later, if i have a 0 then they are equal boards, something like that.

Share this post

Link to post
Share on other sites

int i = 7;

bool bit_01 = !!(i & 0x01);
bool bit_02 = !!(i & 0x02);
bool bit_03 = !!(i & 0x04);
bool bit_04 = !!(i & 0x08);
bool bit_05 = !!(i & 0x10);
bool bit_06 = !!(i & 0x20);
bool bit_07 = !!(i & 0x40);
bool bit_08 = !!(i & 0x80);
// pattern continues...
bool bit_24 = !!(i & 0x01000000);
bool bit_25 = !!(i & 0x02000000);
bool bit_26 = !!(i & 0x04000000);
bool bit_27 = !!(i & 0x08000000);
bool bit_28 = !!(i & 0x10000000);
bool bit_29 = !!(i & 0x20000000);
bool bit_30 = !!(i & 0x40000000);
bool bit_31 = !!(i & 0x80000000);

bit_01, bit_02, bit_03 would be true, the rest false.
7d = 0x00000007 = 00000000 00000000 00000000 00000111b

You can also shift a 1 into place
(1<<8) = 0x80
(1<<31) = 0x80000000

Share this post

Link to post
Share on other sites
Original post by RAZORUNREAL
Is the !! necessary? If you were using C style BOOLs it would be, but I think C++ bools can only be 0 or 1.

(i & 0x20) == 0x20
That'll give you a warning about truncation from an int to a bool. And the bool is likely to end up as 0 all the time, since the lowest bit of 0x20 isn't set (and in theory that's the only bit that matters to a bool). Using the !! converts it to a bool.
Equally, you could do:
bool bit_06 = (i & 0x20)?true:false;
But the double !! looks nicer and is perhaps more efficient.

Share this post

Link to post
Share on other sites
To extract a random bit use this:

#include <cassert>
#include <limits>

template<typename T>
bool get_bit(T in,unsigned int bit)
// Note: 0 is the first bit

assert( std::numeric_limits<T>::digits > bit);
return !!(in & 1<< bit );

Share this post

Link to post
Share on other sites
Thanks Evil Steve. Seems a bit inconsistant that it branches on anything >0 but casting to a bool just depends on the least significant bit. Oh well.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!