Archived

This topic is now archived and is closed to further replies.

Zeraan

What is bit operators and how are they used?

Recommended Posts

In GDNet Lounge, a guy said something about interviewing with a question on bit stuff. I don''t understand how this stuff works: | & << >> etc. If someone can either explain those operators clearly (I know that << and >> shifts the bits, but I don''t understand exactly how they work) or point me to a site with tutorial on bit manipulation, I''d apprecriate this. I think I can optimize some of my game''s parts with bit manipulating if I think this is what it is...

Share this post


Link to post
Share on other sites
I dug around and found these old notes:


AND &
- operates like a 2 bit truth table
- if either bit is zero, yield bit is zero too
- both bits must be one for the yield bit to be one

0011
& 0101
----
0001

-------------------------------------------------

XOR ^
- bitwise exclusive OR
- returns 1 when either bit but not both are 1

0011
^ 0101
----
0110

- an XOR applied twice yields the original operand

-------------------------------------------------

OR |

- two bits ORed yield 1 if either or both bits are 1 and 0 if both bits are 0
- used to combine bits from different variables into a single variable

0011
| 0101
----
0111

-------------------------------------------------

RIGHT SHIFT >>
- moves each bit in operand to the right the indicated number of places
- shifting right one bit same as dividing operand by 2

-------------------------------------------------

BITWISE LEFT <<
- always shifts in 0 bits from the right discarding left most bits
- each bit shifted is equivalent to multiplying by 2
- so shifting left 3 bits is like muliplying by 8

-------------------------------------------------

BITWISE COMPLEMENT ~ (unary)
- reverses each bit
- complementing a number twice returns the orginal number


[edited by - lessbread on July 18, 2002 2:10:24 AM]

Share this post


Link to post
Share on other sites