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

Bitwise operators

Started by CTRL_ALT_DELETE Apr 10, 2001 at 10:03 PM 7 replies 450+ views
Original Post
CTRL_ALT_DELETE
CTRL_ALT_DELETE
OK...so the C++ book I have never even mentions the uses of a bitwise operator. Can someone point to web site or *.txt file that would reveal the mysteries of bitwise manipulation to me? (I feel as if I should know them. Thanks.
Clash Rocker
Clash Rocker
I don''t know if I can be of much help, but I try and think
of AND (&) as returning a bit or bit pattern, and OR (|) as
setting a bit or bit pattern on/off.

You use masks to operate on some data.
short foo = 0x45;
short bar;
short mask_high = 0x007F; //Higher 7 bits. 0000000001111111
short mask_low = 0x3F80; //"Lower" 7 bits. 0011111110000000

Now say you want to get the high 7 bits of foo...
bar = foo &= mask_high;

That''s really as far as I''ve gotten.

www.omf.com == fightinh
python_regious
python_regious
The bitwise operators are the same as the boolean operations that you can do in boolean algebra. If you don''t know that, then heres a quick overview.

AND ("&")

Sets a bit "1" if boths bits are 1, 0 otherwise.
eg.

A B C
0 0 0
0 1 0
1 0 0
1 1 1

where A and B are inputs and C is the output.

OR ("|")

Sets a bit to "1" if either of the input bits are 1, 0 otherwise.

A B C
0 0 0
0 1 1
1 0 1
1 1 1

NOT ("^")

Inverts each of the bits.

A C
0 1
0 1
1 0
1 0

Exclusive OR (dunno if C supports this)

Sets a bit to "1" if either but not both of the input bits is set.

A B C
0 0 0
0 1 1
1 0 1
1 1 0

This is also eqivelent to:

(A+B).NOT(A.B)

If at first you don't succeed, redefine success.
T Bag
T Bag
quote:
Original post by python_regious
AND ("&")
OR ("|")
NOT ("^")
Exclusive OR (dunno if C supports this)



Huh. Where I come from, "^" is XOR and NOT is written "!".

Bitwise operators are like regular boolean operators, except bitwise. They work on each binary digit.

----------hang ten,
T*Bag
Nova Z
Nova Z
~ inverts all the bits. In C/C++ anyway.
------------------------------------------If you assume there is only one path to a goal, then you have only found the path to failure.
python_regious
python_regious
WTF?? "!" is not a NOT gate at all, what are you talking about? A NOT gate inverts all the bits to what they are not, "!" is a logical operator. And yes "^" is the XOR operator,"~" is the NOT gate ( in C++ it''s called the complement ).
If at first you don't succeed, redefine success.
T Bag
T Bag
sorry, I have never used a bitwise not, just figured it was the same.

----------hang ten,
-> T*Bag
python_regious
python_regious
Just read my post, and it was a bit mean, sorry bout that
If at first you don't succeed, redefine success.
T Bag
T Bag
no harm done

----------hang ten,
-> T*Bag

Topic Locked

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

Sign in to reply to this topic.