Python Bitwise Operators - Building Blocks of Advanced Programming

Rinu Gour
PythonFlood
Published in
7 min readMay 30, 2023

--

Python provides a set of powerful operators for performing bitwise operations on binary numbers. These operators allow you to manipulate individual bits within a number, providing a range of tools for efficient data handling and algorithmic operations.

Bitwise operators are useful when working with data sets, cryptography, and networking. They can help improve code performance and achieve faster, more efficient results.

Python BItwise Operator

Let’s talk about Python bitwise operators! Bitwise operators are useful in programming. They can manipulate the individual bits of a number. In Python, there are several bitwise operators that you can use, including:

  • & (AND operator)
  • | (OR operator)
  • ^ (XOR operator)
  • ~ (NOT operator)
  • << (left shift operator)
  • (right shift operator)

Let’s explore each of these operators in more detail, and use some real-life examples to help illustrate their usage.

Basics of Binary System

Binary system is a numbering system that uses only two digits, 0 and 1, to represent all numbers. In contrast, the decimal system (base 10) that we use in everyday life uses ten digits, 0 through 9.

In binary system, each digit represents a power of 2, starting with the rightmost digit representing 2⁰ the next digit representing 2¹, and so on. For example, the binary number 1101 can be converted to decimal as follows:

12³ + 12² + 02¹ + 12⁰ = 8 + 4 + 0 + 1 = 13

In addition to decimal and binary, there are other numbering systems such as octal (base 8) and hexadecimal (base 16) that are commonly used in computer science.

These numbering systems use 8 and 16 digits respectively to represent all numbers Understanding the basics of binary system is important in computer science because all digital devices, including computers, use binary system to represent and process information.

In fact, every piece of data that a computer stores or processes is ultimately represented as a sequence of binary digits.

1. The ‘&’ Operator

The & operator performs a bitwise AND operation on two numbers. This operator compares the binary representation of two numbers bit by bit, and returns a new number where each bit is 1 only if the corresponding bits of the input numbers are also 1.

For example, let’s say we have two numbers, 5 and 3. In binary, these numbers are represented as 101 and 011, respectively. If we perform a bitwise AND operation on these numbers using the & operator, we get the result 001, which is equal to the decimal value 1.

In real life, bitwise AND can be used to check if a certain condition is true or false. For example, let’s say you want to check if a number is even or odd. You can use the bitwise AND operator to check if the last bit of the number is 0 (even) or 1 (odd).

Here’s an example:

# Check if a number is even or odd
def is_even(number):
return number & 1 == 0

# Test the function
print(is_even(5)) # Output: False
print(is_even(6)) # Output: True

2. The ‘ | ‘ operator

The | operator performs a bitwise OR operation on two numbers. This operator compares the binary representation of two numbers bit by bit, and returns a new number where each bit is 1 if the corresponding bits of either input number are 1.

For example, let’s say we have two numbers, 5 and 3. In binary, these numbers are represented as 101 and 011, respectively. If we perform a bitwise OR operation on these numbers using the | operator, we get the result 111, which is equal to the decimal value 7.

Here’s an example:

a = 0b101  # Binary representation of 5
b = 0b110 # Binary representation of 6

# Bitwise OR operation
c = a | b

print(c) # Output: 7 (binary representation 0b111)

3. The ‘ ^ ’ operator

The ^ operator performs a bitwise XOR operation on two numbers. This operator compares the binary representation of two numbers bit by bit, and returns a new number where each bit is 1 if the corresponding bits of the input numbers are different.

For example, let’s say we have two numbers, 5 and 3. In binary, these numbers are represented as 101 and 011, respectively. If we perform a bitwise XOR operation on these numbers using the ^ operator, we get the result 110, which is equal to the decimal value 6.

Here’s an example:

a = 0b101  # Binary representation of 5
b = 0b110 # Binary representation of 6

# Bitwise XOR operation
c = a ^ b

print(c) # Output: 3 (binary representation 0b011)

4. The ‘ << ‘ operator

The << operator performs a left shift on a number. This operator takes two operands: the first operand is the number to be shifted, and the second operand is the number of bits to shift it by.

The operator shifts all the bits of the first operand to the left by the number of bits specified in the second operand, and fills the empty bits with 0s.

For example, let’s say we have the number 5. In binary, this number is represented as 101. If we perform a left shift operation on this number using the << operator with a shift value of 2, we get the result 20 (assuming we’re using 32-bit integers). This is because the binary representation of 20 is 00000000000000000000000000010100, which is the binary representation of 5 shifted to the left by 2 bits.

In real life, left shift can be used to perform multiplication and division by powers of 2. For example, let’s say you want to multiply a number by 4. You can do this by left shifting the number by 2 bits, because 4 is equal to 2 raised to the power of 2.

Here’s an example:

# Multiply a number by 4 using left shift
num = 5
result = num << 2 # 5 * 2^2 = 20
print(result)

5. The ‘ >> ‘ operator

The >> operator performs a right shift on a number. This operator takes two operands: the first operand is the number to be shifted, and the second operand is the number of bits to shift it by.

The operator shifts all the bits of the first operand to the right by the number of bits specified in the second operand, and fills the empty bits with 0s if the number is positive, or 1s if the number is negative.

An example scenario is having the number -8. In binary, this number is represented as 11111111111111111111111111111000 (assuming we’re using 32-bit integers). If we perform a right shift operation on this number using the >> operator with a shift value of 2, we get the result -2. This is because the binary representation of -2 is 11111111111111111111111111111110, which is the binary representation of -8 shifted to the right by 2 bits.

In real life, right shift can be used to perform division and modulus by powers of 2. For example, let’s say you want to divide a number by 8. You can do this by right shifting the number by 3 bits, because 8 is equal to 2 raised to the power of 3.

Here’s an example:

# Divide a number by 8 using right shift
num = 64
result = num >> 3 # 64 / 2^3 = 8
print(result)

6. The Compound Operator

In Python, a compound operator is a shorthand way of combining an arithmetic operation with an assignment. It allows you to modify the value of a variable in-place by performing an operation on the variable’s current value and a second operand, then assigning the result back to the variable.
The most common compound operators in Python are:

  • += : Addition assignment
  • -= : Subtraction assignment
  • *= : Multiplication assignment
  • /= : Division assignment
  • //= : Floor division assignment
  • %= : Modulus assignment
  • **= : Exponentiation assignment
  • &= : Bitwise AND assignment
  • |= : Bitwise OR assignment
  • ^= : Bitwise XOR assignment
  • <<= : Left shift assignment
  • >>= : Right shift assignment

Here is an example:

# Addition assignment
x = 5
x += 3
print(x) # Output: 8

# Subtraction assignment
x = 5
x -= 3
print(x) # Output: 2

# Multiplication assignment
x = 5
x *= 3
print(x) # Output: 15

# Division assignment
x = 10
x /= 2
print(x) # Output: 5.0

# Floor division assignment
x = 10
x //= 3
print(x) # Output: 3

# Modulus assignment
x = 10
x %= 3
print(x) # Output: 1

# Exponentiation assignment
x = 2
x **= 3
print(x) # Output: 8

# Bitwise AND assignment
x = 5
x &= 3
print(x) # Output: 1

# Bitwise OR assignment
x = 5
x |= 3
print(x) # Output: 7

# Bitwise XOR assignment
x = 5
x ^= 3
print(x) # Output: 6

# Left shift assignment
x = 5
x <<= 2
print(x) # Output: 20

# Right shift assignment
x = 10
x >>= 1
print(x) # Output: 5

Conclusion

In conclusion, understanding bitwise operators in Python can be a valuable tool for any programmer. Bitwise operators provide efficient ways to manipulate binary data. They can help streamline your code, reduce processing time, and improve the performance of your application.

By mastering these operators, you can unlock new possibilities for data analysis, network programming, and more.

This article aims to introduce Python’s left shift and right shift operators and encourage you to explore the other bitwise operators available in Python.

--

--

Data Science Enthusiast | Research writer | Blogger | Entrepreneur