by metulburr » Sun Sep 15, 2013 11:25 pm
bit manipulation, what else?
They work on the binary bits of numbers. Take 56 which is 00110001 in binary, and 235 which is 11101011 in binary.
First, you have bitwise AND(&). This compares each bit. If the bits from both of the numbers are 1, then the resulting bit is 1. Otherwise, the resulting bit is 0. For example:
56 00111000
235 11101011
&-------
40 00101000
Next, you have bitwise OR(|). If either bit is 1, then the resulting bit is 1.
56 00111000
235 11101011
|-------
251 11111011
Also, you have bitwise XOR(^). If only one of the bits is 1, then the resulting bit is 1 (if both are 1, or neither are 1, then the resulting bit is 0).
56 00111000
235 11101011
^-------
211 11010011
The final bitwise operator is bitwise NOT (~). This works on only one operand - it simply flips the bits.
56 00111000
~-------
199 11000111
235 11101011
~-------
20 00010100