CodeSutraHub

Bitwise Operators in Python

Data processing can be done very efficiently at the bit level. In this topic, we will learn about the operators used to perform bit-level operations.

Bitwise Operators in Python

Bitwise operators are used in Python to perform bit-level operations. Python provides six bitwise operators. They are explained below.

Bitwise Operators and Their Meaning

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Left Shift
>> Right Shift

Bitwise AND Operator (&) in Python

  • The output bit is 1 only if both corresponding bits of operands are 1.
  • If any bit is 0, the result is 0.

Example:
a = 8
b = 13
print(a & b)

Binary Calculation:
8  = 00001000
13 = 00001101
AND = 00001000 → 8

Output: 8

Bitwise OR Operator (|) in Python

  • If at least one corresponding bit is 1, the output is 1.
  • If both bits are 0, the result is 0.

Example:
a = 8
b = 13
print(a | b)

Binary Calculation:
8  = 00001000
13 = 00001101
OR = 00001101 → 13

Output: 13

Bitwise XOR Operator (^) in Python

  • The output bit is 1 if the corresponding bits are different.
  • If both bits are the same (0 or 1), the result is 0.

Example:
a = 8
b = 13
print(a ^ b)

Binary Calculation:
8  = 00001000
13 = 00001101
XOR = 00000101 → 5

Output: 5

Bitwise Complement Operator (~) in Python

  • It is a unary operator (works on only one operand).
  • It inverts every bit (0 → 1, 1 → 0).

Example:
b = 13
print(~b)

Binary Calculation:
13  = 00001101
~13 = 11110010

Output: -14 (due to 2's complement representation)

What is 2's Complement?

Two’s complement is a method used by computers to represent negative numbers in binary. It makes arithmetic operations like addition and subtraction easier.
Steps to find 2’s Complement:
  1. Write the number in binary.
  2. Invert all the bits (0 → 1, 1 → 0).
  3. Add 1 to the inverted bits.

Example:
Represent -6 in 4-bit 2’s complement

+6  = 0110
Invert = 1001
Add 1  = 1010

So, -6 = 1010
Uses of 2's Complement
  • Only one zero representation exists.
  • Simplifies arithmetic operations.
  • Easy hardware implementation.

Right Shift Operator (>>) in Python

  • Shifts bits to the right.
  • Each right shift is equivalent to dividing by 2.

Example:
a = 8
print(a >> 1)

Binary Calculation:
8     = 00001000
8 >> 1 = 00000100 → 4

Output: 4

Left Shift Operator (<<) in Python

  • Shifts bits to the left.
  • Each left shift is equivalent to multiplying by 2.

Example:
a = 8
print(a << 1)

Binary Calculation:
8      = 00001000
8 << 1 = 00010000 → 16

Output: 16

Example of Bitwise Operators in Python


# Example of Bitwise Operators in Python
a = 8
b = 13

print("a & b =", a & b)
print("a | b =", a | b)
print("a ^ b =", a ^ b)
print("~b    =", ~b)
print("a << 1 =", a << 1)
print("a >> 1 =", a >> 1)
Output:
a & b = 8
a | b = 13
a ^ b = 5
~b = -14
a << 1 = 16
a >> 1 = 4

What We Have Learned So Far

  • Learned about bitwise operators
  • Wrote programs using bitwise operators
  • Relational and Logical Operators will be covered in the next topic

Below Video Explains Bitwise Operators in Python