Skip to content

Python Bitwise Operators

Unleashing the Power of Bitwise Operators in Python

Section titled “Unleashing the Power of Bitwise Operators in Python”

Bitwise operators in Python offer a low-level, efficient means of manipulating individual bits within integers. These operators enable you to perform bit-level operations, making them invaluable in scenarios involving hardware interfaces, cryptography, and other applications where binary representation matters. In this comprehensive guide, we’ll explore the world of bitwise operators, their syntax, and their applications in Python programming.

The following table lists the bitwise operators in Python:

OperatorDescriptionExample
&Performs bitwise AND on operandsx & y
|Performs bitwise OR on operandsx | y
^Performs bitwise XOR on operandsx ^ y
~Performs bitwise NOT on operands~x
<<Performs bitwise left shift on operandsx << y
>>Performs bitwise right shift on operandsx >> y

The bitwise AND operator (&) performs a bitwise AND operation on the binary representations of two integers. The result of this operation is a new integer whose binary representation is the result of the bitwise AND operation. The following example demonstrates how to use the bitwise AND operator in Python:

operators.py
# Bitwise AND operator
x = 10
y = 5
z = x & y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
0

In the above example, we have used the bitwise AND operator to perform a bitwise AND operation on the binary representations of two integers x and y. The result of this operation is a new integer whose binary representation is the result of the bitwise AND operation. The result of the bitwise AND operation is then assigned to the variable z. The value of z is then printed to the console.

The bitwise OR operator (|) performs a bitwise OR operation on the binary representations of two integers. The result of this operation is a new integer whose binary representation is the result of the bitwise OR operation. The following example demonstrates how to use the bitwise OR operator in Python:

operators.py
# Bitwise OR operator
x = 10
y = 5
z = x | y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
15

In the above example, we have used the bitwise OR operator to perform a bitwise OR operation on the binary representations of two integers x and y. The result of this operation is a new integer whose binary representation is the result of the bitwise OR operation. The result of the bitwise OR operation is then assigned to the variable z. The value of z is then printed to the console.

The bitwise XOR operator (^) performs a bitwise XOR operation on the binary representations of two integers. The result of this operation is a new integer whose binary representation is the result of the bitwise XOR operation. The following example demonstrates how to use the bitwise XOR operator in Python:

operators.py
# Bitwise XOR operator
x = 10
y = 5
z = x ^ y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
15

In the above example, we have used the bitwise XOR operator to perform a bitwise XOR operation on the binary representations of two integers x and y. The result of this operation is a new integer whose binary representation is the result of the bitwise XOR operation. The result of the bitwise XOR operation is then assigned to the variable z. The value of z is then printed to the console.

The bitwise NOT operator (~) performs a bitwise NOT operation on the binary representation of an integer. The result of this operation is a new integer whose binary representation is the result of the bitwise NOT operation. The following example demonstrates how to use the bitwise NOT operator in Python:

operators.py
# Bitwise NOT operator
x = 10
z = ~x
print(z)

Output:

command
C:\Users\Your Name> python operators.py
-11

In the above example, we have used the bitwise NOT operator to perform a bitwise NOT operation on the binary representation of an integer x. The result of this operation is a new integer whose binary representation is the result of the bitwise NOT operation. The result of the bitwise NOT operation is then assigned to the variable z. The value of z is then printed to the console.

The bitwise left shift operator (<<) performs a bitwise left shift operation on the binary representation of an integer. The result of this operation is a new integer whose binary representation is the result of the bitwise left shift operation. Basically, the bitwise left shift operator shifts the bits of an integer to the left by a specified number of bits and fills the empty bits with zeros.

The following example demonstrates how to use the bitwise left shift operator in Python:

operators.py
# Bitwise left shift operator
x = 10
y = 2
z = x << y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
40

In the above example, we have used the bitwise left shift operator to perform a bitwise left shift operation on the binary representation of an integer x. The result of this operation is a new integer whose binary representation is the result of the bitwise left shift operation. The result of the bitwise left shift operation is then assigned to the variable z. The value of z is then printed to the console.

The bitwise right shift operator (>>) performs a bitwise right shift operation on the binary representation of an integer. The result of this operation is a new integer whose binary representation is the result of the bitwise right shift operation. Basically, the bitwise right shift operator shifts the bits of an integer to the right by a specified number of bits and fills the empty bits with zeros.

The following example demonstrates how to use the bitwise right shift operator in Python:

operators.py
# Bitwise right shift operator
x = 10
y = 2
z = x >> y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
2

In the above example, we have used the bitwise right shift operator to perform a bitwise right shift operation on the binary representation of an integer x. The result of this operation is a new integer whose binary representation is the result of the bitwise right shift operation. The result of the bitwise right shift operation is then assigned to the variable z. The value of z is then printed to the console.

Bitwise operators line up the two numbers bit by bit and combine each column: & (AND) keeps a 1 only where both bits are 1, | (OR) where either is 1, and ^ (XOR) where exactly one is 1. Watch the result form column by column as the operator cycles:

sketch Bitwise operators work bit by bit p5.js
AND keeps 1s common to both, OR keeps 1s in either, XOR keeps 1s in exactly one.

Bitwise operators in Python offer a powerful set of tools for working with individual bits in integers. While these operators may not be as commonly used in everyday programming, they become crucial in scenarios where low-level bit manipulation is required.

As you advance in your Python programming journey, experiment with bitwise operators, explore their applications in real-world scenarios, and leverage their efficiency for tasks involving binary representation. For more insights and practical examples, check out our tutorials on Python Central Hub!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading