Skip to main content
gamedev.net
🔒 Locked

Working with binary

Started by tiagocc0 Apr 5, 2006 at 9:02 PM 21 replies 2k views
Original Post
tiagocc0
tiagocc0
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.
Tradone
Tradone
tutorial about what
and what is it exactly that you wish to accomplish.
tiagocc0
tiagocc0
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.
tiagocc0
tiagocc0
thanks, i will take a look at that
Shannon Barber
Shannon Barber
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
RAZORUNREAL
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.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
Evil Steve
Evil Steve
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.
CTar
CTar
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 );}
RAZORUNREAL
RAZORUNREAL
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!
CTar
CTar
Quote:
Original post by RAZORUNREAL
but casting to a bool just depends on the least significant bit.

No, everything but zero, null pointer and null member pointer will be converted to true.
Quote:
From the C++ Standard 2003, 4.12 Boolean conversions
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.


Zahlman
Zahlman
Quote:
Original post by CTar
Quote:
Original post by RAZORUNREAL
but casting to a bool just depends on the least significant bit.

No, everything but zero, null pointer and null member pointer will be converted to true.
Quote:
From the C++ Standard 2003, 4.12 Boolean conversions
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.


!!

You mean if an enumeration starts at zero, the first value is converted to true anyway? x.x
CTar
CTar
Quote:
Original post by Zahlman
Quote:
Original post by CTar
Quote:
Original post by RAZORUNREAL
but casting to a bool just depends on the least significant bit.

No, everything but zero, null pointer and null member pointer will be converted to true.
Quote:
From the C++ Standard 2003, 4.12 Boolean conversions
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.


!!

You mean if an enumeration starts at zero, the first value is converted to true anyway? x.x


No, the first value (assuming the enumerations starts from 0) is a zero value and therefore will be converted to false.
Alpha_ProgDes
Alpha_ProgDes
Quote:
Original post by Shannon Barber
*** Source Snippet Removed ***

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


I still the don't get the !!.
(i & 0x20) == !!(i & 0x20) doesn't it?
!! == not not == true
??
Beginner in Game Development?  Read here. And read here.  
RAZORUNREAL
RAZORUNREAL
Quote:
Original post by Evil Steve
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).


Quote:
Original post by CTar
No, everything but zero, null pointer and null member pointer will be converted to true.


I... think I'm gonna believe CTar. In which case I think this:
bool bit_06 = (bool)(i & 0x20);
Is much less likely to make a maintainer murderous than
bool bit_06 = !!(i & 0x20);

Quote:
Original post by Alpha_ProgDes
I still the don't get the !!.
(i & 0x20) == !!(i & 0x20) doesn't it?
!! == not not == true
??


The problem is that (i & 0x20) won't give you 1. It'll give you... um... 32? What the !! does is make sure it's 0 or 1. And confuses most people. The first not will turn 32 into 0 or 0 into 1 you see. So if you not it again, either the 32 has become 1, or the 0 is back to 0, and you have what you want.
___________________________________________________David OlsenIf I've helped you, please vote for PigeonGrape!
smart_idiot
smart_idiot
Pop quiz, what does this program print?

#include <iostream>int main(){    int n_a = 1, n_b = 256;    bool a, b;    a = n_a;    b = n_b;    if(a == b)        std::cout << "cheese!" << std::endl;    else        std::cout << "peppers!" << std::endl;}
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
BeerNutts
BeerNutts
peppers!

(because bool is only 8-bits, 256 = 0x100 which, when cast down to 8-bits = 0).

I personally just use the values for a check, like this:
#define BIT_SET(xVar, xBit) ((xVar) & (1 << (xBit)))if (BIT_SET(SomeVariable, BitPosition)){}else{}
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)
nobodynews
nobodynews
I figured it would be cheese, because both 1 and 256 are non zero then it would assign a true value to both a and b. So I tried it out (gcc version 3.3 on linux) and got cheese. Of course, I'm not going to assume this will be true on all compilers without reading what the standard says about it.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
CTar
CTar
Quote:
Original post by BeerNutts
peppers!

(because bool is only 8-bits, 256 = 0x100 which, when cast down to 8-bits = 0).

No. Have you checked that on a compiler? If you have, which one? Bool might be 8 bits on your platform, but it wont ever look like a number it can only hold true and false, and in undefined cases it can look like it's something else.

#include <iostream>int main(){    // Integers are able to hold the values, nothing strange happens here    int n_a = 1, n_b = 256;    // Two bools are declared their value is undefined    // it could even be something else than true or false (see 3.9.1 in the standard).    bool a, b;    // The compiler checks:    // - Is n_a a zero value? No    // - Is n_a a null pointer value? No    // - Is n_a a null member pointer value? No    // Well then it's set to true.    a = n_a;    // Again the compiler checks:    // - Is n_b a zero value? No    // - Is n_b a null pointer value? No    // - Is n_b a null member pointer value? No    // Well then it's set to true.    b = n_b;    // Both is true, so cheese    if(a == b)        std::cout << "cheese!" << std::endl;    else        std::cout << "peppers!" << std::endl;}
BeerNutts
BeerNutts
Well, in our work environment (an embedded MIPS platform using codewarrior for the compiler), we have issues with people using bool and doing something like this:

bool bVar;bVar = (SomeVar & (1 << 20));if (bVar)...


Where bool is an 8-bit value but SomeVar is 32-bits and the result should be true, but isn't. So, I dunno.

EDIT:

After further review, I'm confusing the bool with a locally defined bool. I checked the disassembly of the follwoing and got the further assembly (where na and nb are passed in integers, and register-wise, s0 = nb, and s1 = na):

; 282: b = (na & (1 << nb));
; 283:
;
0x00000038 0x24030001 li v1,1
0x0000003C 0x02031804 sllv v1,v1,s0
0x00000040 0x02231824 and v1,s1,v1
0x00000044 0x0003182B sltu v1,$0,v1
;
; 284: if (b)
;
0x00000048 0x10600005 beq v1,$0,*+24 ; 0x00000060
0x0000004C 0x00000000 nop

[Edited by - BeerNutts on April 7, 2006 4:57:33 PM]
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)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.