Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bitwise Operation

Started by Supaflyfrank Jun 4, 2006 at 9:49 PM 5 replies 850+ views
Original Post
Supaflyfrank
Supaflyfrank
Ok my book has code to show a number in binary and I am only confused at one part if(u&t) When does that equal true and when does it equal false. When it makes a change it equals true? And if it doesnt it equals false? I thought it just outputs a resulting binary sequence.

void show_binary(unsigned int u)
{
  int t;
  for(t=128;t>0;t=t/2)
   if(u&t) cout << "1 ";
   else cout << "0 ";

  cout << "\n";
}

JohnBolton
JohnBolton
The AND operation results in 1 if both of the operands are 1. Now, in bitwise operations, this is done for each bit.

However, the condition in and if-statment uses the entire value, not just any single bit. If the value of the condition is not 0 (i.e. if any bit is 1), then the condition is true. If the value is 0 (i.e. all bits are 0), then the condition is false.

For example, "if ( 5 & 4 ) ..." will take the true branch because 5 (101) & 4 (100) = 4 (100). Since a bit is set in the result, the condition is true.

In your sample code, take a look at the values of t. Notice that they are 128, 64, 32, ... What are those values in binary? What is the loop doing?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Supaflyfrank
Supaflyfrank
ok so I was right if basically any bit was changed it will return true
Trienco
Trienco
Quote:
Original post by Supaflyfrank
ok so I was right if basically any bit was changed it will return true


Nothing in this code is actually changing any bits in the first place. t has power of two, ie. it always has only one bit set. With &, t is used as a mask, so if this ONE bit in u is 1, the result is non-zero (true).

But then, I don't quite see the point of using unsigned int, but only look at the last byte. Even if it's meant to avoid endian confusion, it would make more sense to make u an unsigned char in the first place.

If you insist on thinking about changed bits, then this is true if this one particular bit is NOT changed in either number (ie. was 1 in both).
f@dzhttp://festini.device-zero.de
Supaflyfrank
Supaflyfrank
Ok what you say makes some sense to me. And I understand that the other bits are masked by using zeros but what doesnt make sense to me if you have something like

1 In Binary: 00000001
254 In Binary: 11111110
and then
boo ifs = (1&254)
if you print out ifs it displays 0 (false)

65 In Binary: 01000001
254 In Binary: 11111110
and then
boo ifs = (1&65)
if you print out ifs it displays 1 (true)

127 In Binary: 01111111
254 In Binary: 11111110
and then
boo ifs = (127&254)
if you print out ifs it displays 0 (false)

reason i use bool ifs is to make it a true and false statement as if it was a if statement.

For the 1 254 Im guessing because the other values have to be 0 you only are looking at the first bit which inturn is zero cause the first bit in 254 is 0

For the 65 254 Because we are basically ignoring all the bits except the 7th and first because of a mask. And then the 7th bit in 65 and the 7th bit in 254 is 1 so I can see the result being 1 (true) but then the first bit in 65 is 1 and the first bit in 254 is 0 so the result of that AND statement would be 0. So then how come that entire sequence returns true?

For the 127 and 254 everything is true except the last bit and everything is true except the first bit so how come the result of that AND is 0 (false)

I understand from a bit by bit bases why you might get true and false but in a multiple bit sequence what determines if the entire sequence is true. I understand that AND really doesnt change the orginal. They say you use masks to only minipulate only a certain section but why cant you just modify it directly.
Making it so you can only see a certain group of numbers such as:
10100101 number
00000101 mask want to see the first 3 digits
-------- Using And
00000101 What did you truly accomplish?

If you want to modify the second digit turning it on so in turn ouu could use OR like:
10100101
00000111
--------
10100111
Basically getting the results you want.

There is no bit or byte data type. the smallest data is bool which Im guessing is basically 1 bit but I dont get why you need to mask anything for the fact you can just modify it directly.
KulSeran
KulSeran
65 & 254 => 0100 0000
127 & 254 => 0111 1110
both eval to true because both are non-zero

1 & 254 => 0000 0000
is == 0, so it evals to false

A bool is often 8bits, unless you specify to the compile to pack several bools into one 8-bit segment.

A char is also 8bits, but you can read all 8 of the bits, where a bool can only be treated as one bit.
So, with, in your case above, an unsigned int, you have likely 32bits of data.
with 32 bits you can store 32 bools.
So, using a technique called bit-flags you can store lots of information in one integer.
With bit-wise masks you can get each value. This is how API's like directX and OpenGL store some of thier state information.
It can also be seen in places like file attribute flags.

ie.
read_only 0001
archive 0010
system 0100
hidden 1000

so a file with flag = 1011 is read only, archive, and hiden
so (flag & read_only) evaluates to true, since it is read only.
if flag = 1000, then you can test (flag & hidden) and get true, but no other mask is true, so you know that it is not read only, system or archive.

[Edited by - KulSeran on June 5, 2006 6:52:24 PM]
Trienco
Trienco
Quote:
Original post by Supaflyfrank
Making it so you can only see a certain group of numbers such as:
10100101 number
00000101 mask want to see the first 3 digits
-------- Using And
00000101 What did you truly accomplish?


Nothing, because the mask would have to be 00000111 and not 00000101 (why would you "turn off" the second bit if you want to look at all three?)

f@dzhttp://festini.device-zero.de

Topic Locked

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

Sign in to reply to this topic.