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

## 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 on other sites
and what is it exactly that you wish to accomplish.

##### 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 on other sites
What you want are bitwise operators, and C Programming has an intro to them.

##### Share on other sites
thanks, i will take a look at that

##### 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 on other sites
Is the !! necessary? If you were using C style BOOLs it would be, but I think C++ bools can only be 0 or 1.

##### Share on other sites
Quote:
 Original post by RAZORUNREALIs 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 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 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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633384
• Total Posts
3011597
• ### Who's Online (See full list)

There are no registered users currently online

×