Working with binary

Started by
21 comments, last by Shannon Barber 18 years ago
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.
Advertisement
tutorial about what
and what is it exactly that you wish to accomplish.
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.
What you want are bitwise operators, and C Programming has an intro to them.
XBox 360 gamertag: templewulf feel free to add me!
thanks, i will take a look at that
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Is the !! necessary? If you were using C style BOOLs it would be, but I think C++ bools can only be 0 or 1.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Quote: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.
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 );}
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.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!

This topic is closed to new replies.

Advertisement