Bit Manipulation Tutorial
Bit manipulation is a great way to save values and compress data into it's smallest form possible. Not only does it have it's applications, it's also incredibly fun!
Before we start, you need to be able to read binary. Once you've learned how (or already know how) to read binary, you can continue.
Bit manipulation according to Wikipedia is "the act of algorithmically manipulating bits or other pieces of data shorter than a word.". At it's core, that's all it is. Just like math you preform operations on binary numbers, operations that you can't do in decimal (1, 2, 3... 8, 9)!
The first operation we're going over is the OR function. If either bits are 1, it will return 1. The OR operator in python looks like
|, so that's what we'll use to represent it.
01101001 | 11010011 ----------- 11111011
As you can see, we did
105 | 213 = 251 but in binary. In decimal this makes no sense, but in binary it's clear that
1 | 1 = 1,
1 | 0 = 1, but
0 | 0 = 0. Fairly self-explanatory.
Speaking of self-explanatory, the next operation we're doing is the AND function. The AND function needs both bits to be 1 for it to return 1. We're going to represent the AND function with
01101001 & 11010011 ----------- 01000001
We used the same numbers as before, but got a different result
105 & 213 = 65. To break it down,
0 & 0 = 0,
1 & 0 = 0, but
1 & 1 = 1. Both these operations are crucial to know, and are used all the time in programming.
The next bitwise operator we're going to use is arguably one of the most important, the foundation to many encryption functions, and almost all hash functions. This operator is called XOR, and is the most complex (but still, easy to understand). It returns 1 if the bits are different, and 0 if they are the same. We will use
^ to represent it just like python does.
01101001 ^ 11010011 ----------- 10111010
Once again using the same numbers, we got
105 ^ 213 = 188. Why is this used for encryption? If the user doesn't know the key, or the input, and just have the result, they won't be able to change it back. And unlike AND and OR, if you took the result and applied one of the inputs, you'd get the other input. Now, XOR alone is insecure, but by applying other methods, you can get make it unhackable.
These next 2 operations are also unhackable, because they simply just remove information. LEFT SHIFT and RIGHT SHIFT are very useful to move bits so that they can become more usable. LEFT SHIFT will move all the bits to the left a certain amount of times, and by using python's LEFT SHIFT syntax
<< we can demonstrate this.
01101001 << 2 ----------- 10100100
Let's break this down. All the bits in the equation above were moved 2 to the left. Because there are only 8 bits in a byte, the last 2 bits to the left were simply removed. Then there was a 2 bit gap to the right, which we filled in with 0s. So
105 << 2 = 164. RIGHT SHIFT is the exact same but, you guessed it, to the right. If we moved it
105 3 to the right it will look like:
01101001 >> 3 ----------- 00001101
That's all the bit manipulation for today. Next tutorial, we're going to use this information to checkout real world applications, like masks, and recreate the discord permission system which is all based on bit manipulation! Hope to see you soon!