Categories

# Bit operators

In the beginning, there was the bits, they were the 1s and the 0s. 1s were positive bits, they gave meaning to things, 0s were negative, but necessary. When working together, they could make wonderful things. Let’s look at this group of bits

``0101``

what are these 0s and 1s telling us? Basically this:

``0(8) + 1(4) + 0(2) + 1(0) = 5``

Each 0 or 1 represents a power of 2 value, starting from the right with 1, to the left with 8. If a 1 is present, we sum that value it represents. In this case, we get 4 + 1 = 5. 0101 is the binary representation of the number 5.

We dont count the 0s, but they hold an important role, without them, we couldnt make all of the numbers.

We don’t see the bits much today, but they still exist, and are as important as ever. Still, we can play with them even today, with the use of bit operators.

open up your JS console, let me introduce you to them

### << “Shift left”

``x << y``

shift x by y bits, to the left. same as `x * 2**y`

examples:

``````5 << 1 // "0101" << 1 => "1010" => 10 => 5 * 2**1
10 << 2 // "1010" << 2 => "101000" => 40 => 10 * 2**2 ``````

### >> “Shift right”

``x >> y``

shift x by y bits, to the right

examples:

``````10 >> 1 // "1010" >> 1 => "0101" => 5 => 10 // (2**1)
20 >> 2 // "10100" >> 2 => "00101" => 5 => 20 // (2**2)``````

### & “Bitwise and”

``x & y``

Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it’s 0.

examples:

``````10 & 3 // "1010" & "0011"
// the 1st bits for both are 1 and 0 so => 1 & 0 = 0
// the 2nd bits for both are 0 and 0 so -> 0 & 0 = 0
// the 3rd bits for both are 1 and 1 so => 1 & 1 = 1
// the 4th bits for both are 0 and 1 so -> 0 & 1 = 0
// output => "0010" => 2``````

### | “Bitwise or”

``x | y``

Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it’s 1.

examples:

``````10 | 3 // "1010" | "0011"
// 1st bits 1 or 0 => 1 | 0 => 1
// 2nd bits 0 or 0 => 0 | 0 => 0
// 3rd bits 1 or 1 => 1 | 1 => 1
// 4th bits 0 or 1 => 0 | 1 => 1
// output => "1011" => 11``````

### ~ “Complement”

``~ x``

Returns the complement .A complement is the opposite of what bits you currently have, and is how you write negative numbers. The complement of `1010` is `(11...)0101`

This is the same as `-x -1`

examples:

``````~ 5 //  "(1...)1010" => -5 -1 => -6
~ 10 // "(1...)0101" => -10 -1 => -11``````

Why the (1…)? This is just me giving a notation to all the 1s present before the relevant bits. Becase when writing negative numbers in binary, you flip the 1s with 0s and the 0s with 1s. Imagine we are dealing with 8 bits. You treat patterns from “00000000” to “01111111” as the whole numbers from 0 to 127, and reserve “1xxxxxxx” for writing negative numbers.

A negative number, -x, is written using the bit pattern for (x-1) with all of the bits complemented (switched from 1 to 0 or 0 to 1). So -1 is complement(1 – 1) = complement(0) = “11111111”, and -10 is complement(10 – 1) = complement(9) = complement(“00001001”) = “11110110”. This means that negative numbers go all the way down to -128 (“10000000”).

BitwiseOperators – Python Wiki

Background image by:
https://pc-paramedix.com/the-matrix-binary-code-blue-wallpaper-1920×1080/ 