Bitwise operators
In the previous section, we learned that a bit is a single number in binary. We also know mathematical operations, such as addition, subtraction and multiplication. Well a bitwise operator is like those, but for directly altering bits.
Bitwise operations are priceless, and can be used in most commonly compression and encryption. For example, if you have a byte variable (8 bits) that you use just as = 1 for true or = 0 for false, you can actually pack 7 more "flags" into that byte that also say true or false! But first, we must learn our operations, just like we did back in Elementary school!
OR
We will start off with the bitwise operator OR. To use a bitwise operator, you will need two values. To make things easy, we will use bytes for both values. The OR operator simply means, "If there is a 1 in the bit of the first or second variable, the result has a 1 in that same bit location." Example:
0101 0110 - Byte 1 OR - Operator 0111 1100 - Byte 2 = - Equals 0111 1110 - Byte 3 (result byte)
Notice how the resulting byte has a lot of 1's. Also notice how I wrote it everything vertically - this is because we are comparing only vertically. If we look at the bit on the farthest to the right on one byte, we look on that exact same position on the second byte, and write the result in the same position in the 3rd byte. None of the other bits will ever affect the result. Lets take a look at a much easier example with a smaller series of bits:
abcd - Label 0101 - Byte 1 OR - Operator 0011 - Byte 2 = - Equals 0111 - Byte 3 (result byte)
I labeled the bits up top in case you didn't notice. Lets take a look at bit a first, on the farthest left. In both byte 1 and byte 2, there is a 0. Because there is no 1 at all in the whole column where "a" is, there is no 1 in the result. For b, there is a 1 in byte 1, so the result has a 1 in that same bit location. For c, same thing, but in byte 2. Finally, for d, there is a 1 in both byte 1 and 2. As long as there is a 1 in the bit section of byte 1 or byte 2, the result has a 1. This doesn't matter if there is one 1 bit or two 1 bits.
One more example:
0101 1101 OR 0111 1000 = 0111 1101
As you can see, there are only 2 spots where the bit for byte 1 and byte 2 are both 0, so in the result, there is only 2 bits that = 0.
AND
Now that you know how OR works, AND will be a lot easier. Just like OR, and works the same way - we only look at the bits in the same position, and the bits around it do not affect it. AND works by, "If the bit in byte 1 and byte 2 are both 1, then the result is 1". As you can see, it is a lot like OR - we are looking for 1's. But unlike OR, the difference with AND is that the bit must be 1 for both of the bytes, not just one. Lets take our last example with OR, but use AND:
0101 1101 AND 0111 1000 = 0101 1000
Now we have hardly any 1's in the result! This is because there are only 3 cases where byte 1 and byte 2 have a 1 in the same bit. If there is any 0's at all, the result has a 0. It is kind of like the opposite of the way OR works - with OR, if there is a 1 in either of the bits, the result is 1. For AND, if there is a 0 in either of the bits, the result is 0.
XOR
Our final bitwise operator is XOR, or Exclusive OR. This is like a combination of AND and OR put together. Again, like AND and OR, this works by bits in the same location only. XOR works by, "If there is only one 1, then the result is 1. If there are no 1's, or two 1's, then the result is 0." Lets use our example from last time again:
0101 1101 XOR 0111 1000 = 0010 0101
The result is OR had, minus everything that AND did not have. In a way, you can think of XOR as (OR - AND), though that may just be even more confusing. ;) The easiest way to go is, "If there is one 1, then the result is one, otherwise, the result is 0."