Bitwise operators

Name

Symbol

Syntax

Explanation

Bitwise AND

&

bit_one & bit_two

Resultant bit is 1 if both bits are 1

Bitwise OR

|

bit_one | bit_two

Resultant bit is 1 if either one of the two bits is 1

Bitwise XOR

^

bit_one ^ bit_two

Resultant bit is 1 if both bits have different values

Bitwise NOT

~

~ bit

Resultant bit is simply the inverted bit

Left shift

<<

bits << 3

Remove the most significant (left) bit and add 0 from the right a specified number of times

Right shift

>>

bits >> 3

Remove the least significant (right) bit and add 0 from the left a specified number of times

For demonstration purposes, let’s take two binary numbers, 10001101 and 11110101. Applying the bitwise operators would give the following results:

Bitwise AND operator

Taking the bitwise AND of these binary numbers would result in 10000101. For example, both of the first bits of the binary numbers are 1, and therefore the AND of these bits is also 1. The next bits are 0 and 1 respectively, the AND of which is 0, and so on.

Expression: 0b100011011 & 0b11110101

Bitwise OR operator

Performing a bitwise OR results in 11111101. For example, the first bits are both 1, so the OR of these bits is 1. The next bits are 1 and 0, also resulting in 1, and so on.

Expression: 0b100011011 | 0b11110101

Bitwise XOR operator

The XOR operation on binary_num_one results in 1111000. For instance, the first bits are both equal to 1, so the result is 0. The next bits are the different (i.e., 1 and 0), resulting in 1, and so on.

Expression: 0b100011011 ^ 0b11110101

Bitwise NOT operator

The NOT operation flips each bit in binary_num_one and results in 10001110. For example in binary_num_1, the first bit is 1, so the NOT is 0. The next bit is 0, so the NOT is 1, and so on.

Expression: ~ 0b100011011

Bitwise left shifting operator

Left shifting binary_num_one results in 100011010. This operation effectively multiplies the number by 2 for each shift left. For instance, shifting left by 1 position removes the leftmost bit 1 and adds 0 to the right.

Expression: 0b100011011 << 1

Bitwise right shifting operator

Right shifting binary_num_one results in 1000110. This operation effectively divides the number by 2 for each shift right. For instance, shifting right by 1 position removes the rightmost bit 0 and adds a 0 at the left.

Expression: 0b100011011 >> 1

Code

We can put this effectively into Python code, and you can even change the numbers and experiment with the code yourself! Click the “Run” button to see the output.

Press + to interact
binary_num_one = 0b10001101
binary_num_two = 0b11110101
bitwise_and = binary_num_one & binary_num_two
print(bin(bitwise_and))
bitwise_or = binary_num_one | binary_num_two
print(bin(bitwise_or))
bitwise_xor = binary_num_one ^ binary_num_two
print(bin(bitwise_xor))
bitwise_not = ~binary_num_one
print(bin(bitwise_not))
left_shift = binary_num_one << 1
print(bin(left_shift))
right_shift = binary_num_one >> 1
print(bin(right_shift))