I wrote an FAQ entry about this, but L@D has yet to put it up...*grumbles* lemme try and find a transcript.
ahh...here we go!
Code:
Bit Manipulation/Bit Shifting - Basic overview
================================================
Bit Manipulation/Shifting is the process of taking data, and toying with
the binary of it (the basic 1s and 0s that it's comprised of).
Bit operations in C/C++ are carried out with the following operators:
-----------------------------------------------------------
|Operator Definition |
-----------------------------------------------------------|
| & | Bitwise AND (Not to be confused with &&)|
| | | Bitwise OR (Not to be conused with ||) |
| ^ | XOR (Exclusive OR) |
| << | Shift left |
| >> | Shift right |
| ~ | Unary (One's compliment) |
------------------------------------------------------------
OR is fairly simple:
The OR operator (|) compares two bits and returns true (1) if either or both bits are set to true.
code:--------------------------------------------------------------------------------
(1 | 2)
00000001
| 00000010
-----------------
00000011
--------------------------------------------------------------------------------
It basically just adds the binary of the numbers. It doesn't actually add the numbers though, remember that.
AND is just as easy:
The AND operator (&) compares two bits and returns true (1) if both bits are set to true.
code:--------------------------------------------------------------------------------
(1 & 3)
00000001
& 00000011
------------------
00000001
--------------------------------------------------------------------------------
See what it did? everywhere there was a 1 in the first one and the second, there was a 1 in the resulting number.
XOR is trickier:
The XOR operator (^) compares two bits and returns true (1) if only one of the bits are set to true.
code:--------------------------------------------------------------------------------
(1 ^ 3)
00000001
^ 00000011
------------------
00000010
--------------------------------------------------------------------------------
Anywhere there was a 1 in both numbers, it became a zero.
UNARY is the easiest. It basically means binary opposite:
code:--------------------------------------------------------------------------------
(~1)
~0000000001 = 1111111110
--------------------------------------------------------------------------------
<< and >> (shift left and right) are incredibly simple, and used the most:
000000010 >> 1 = 000000001
000000001 << 1 = 000000010
It basicall goes like this: number >> Amount to shift by
It moves the numbers down by the number you place to the right.
>> Is actually the quickest way to divide by 2
<< Is actually the quickest way to multiply by 2
Another thing about shifts, is that the numbers don't carry! soo:
000000001 >> 1 = 00000000, NOT 10000000
And remember I wrote the numbers out in binary instead of the actual numbers you'd put in there. ( 000000011 is 3, 0000000010 is 2, and 000000001 is 1 binary) So remember, you don't write the numbers in binary, you write them in decimal to use the bit operators.