Python Bitwise Operators

In the last lesson, we learned identity operators. Now letโ€™s learn how Python works with the binary bits inside integers.

Computers store whole numbers using binary digits. A binary digit is called a bit, and each bit is either 0 or 1.

Bitwise operators compare or move these bits directly.

Understanding Binary Numbers

We normally write numbers using digits from 0 to 9. This is called decimal. Binary uses only 0 and 1.

For example, decimal 5 is binary 101.

Decimal: 5
Binary: 101

Python can show the binary form of an integer with bin().

print(bin(5))
print(bin(10))

Output

0b101
0b1010

The 0b at the beginning tells us that the value is written in binary.

Python Bitwise Operators

Python has six main bitwise operators.

OperatorNameExample
&Bitwise AND5 & 3
|Bitwise OR5 | 3
^Bitwise XOR5 ^ 3
~Bitwise NOT~5
<<Left shift5 << 1
>>Right shift5 >> 1

Let us learn them one by one.

Bitwise AND Operator (&)

Bitwise AND gives 1 only when both bits are 1.

5 = 101
3 = 011
---
001 = 1
print(5 & 3)

Output

1

Only the last column contains 1 in both numbers, so the answer is 001.

Bitwise OR Operator (|)

Bitwise OR gives 1 when at least one of the two bits is 1.

5 = 101
3 = 011
---
111 = 7
print(5 | 3)

Output

7

Every column has at least one 1, so the answer is 111, which is decimal 7.

Bitwise XOR Operator (^)

XOR means exclusive OR. It gives 1 when the two bits are different.

5 = 101
3 = 011
---
110 = 6
print(5 ^ 3)

Output

6

If both bits are the same, XOR gives 0. If they are different, it gives 1.

Bitwise NOT Operator (~)

Bitwise NOT flips the bits of a number. Each 1 becomes 0, and each 0 becomes 1.

print(~5)

Output

-6

The negative answer may look surprising. Python stores negative integers in a form called twoโ€™s complement.

For beginners, the easiest rule is:

~x = -(x + 1)

So ~5 is -(5 + 1), which gives -6.

Left Shift Operator (<<)

The left shift operator moves bits to the left and adds zeros on the right.

5 = 101
5 << 1 = 1010 = 10
print(5 << 1)
print(5 << 2)

Output

10
20

For positive integers, shifting left by one position usually multiplies the number by 2.

Right Shift Operator (>>)

The right shift operator moves bits to the right. Bits that move past the end are removed.

10 = 1010
10 >> 1 = 0101 = 5
print(10 >> 1)
print(10 >> 2)

Output

5
2

For positive integers, shifting right by one position usually performs floor division by 2.

Real-Life Example: Permission Flags

Bitwise operators are often used when several yes-or-no settings are stored inside one number.

READ = 1 # 001
WRITE = 2 # 010
DELETE = 4 # 100
permissions = READ | WRITE
print(permissions)
print(bool(permissions & READ))
print(bool(permissions & DELETE))

Output

3
True
False

READ | WRITE combines the two permissions. Then & checks whether a particular permission is present.

We will not need this pattern in every beginner program, but it is common in permissions, colors, hardware control, and low-level data work.

Common Mistakes with Bitwise Operators

Mistake 1: Confusing & with and

and is a logical operator. It works with truthy and falsy values.

& is a bitwise operator. It works with the individual bits of integers.

print(True and False) # False
print(5 & 3) # 1

They have different jobs, so we should not use them as if they were the same operator.

Mistake 2: Expecting ^ to Mean Power

In Python, ^ means bitwise XOR. It does not mean exponentiation.

print(2 ^ 3) # 1
print(2 ** 3) # 8

We use ** when we want to raise a number to a power.

Mistake 3: Forgetting Parentheses

Bitwise expressions can become difficult to read when they are mixed with comparisons or arithmetic.

result = (5 & 3) == 1
print(result)

Output

True

Parentheses make the order clear for us and for anyone reading the code.

Next, we will learn about the if Statement so we can start making decisions in our code.