Skip to content

Python Bitwise Operators

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 & yx & y
||Performs bitwise OR on operandsx | yx | y
^^Performs bitwise XOR on operandsx ^ yx ^ y
~~Performs bitwise NOT on operands~x~x
<<<<Performs bitwise left shift on operandsx << yx << y
>>>>Performs bitwise right shift on operandsx >> yx >> y

Bitwise AND

&& (Bitwise AND) Operator

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)
operators.py
# Bitwise AND operator
x = 10
y = 5
z = x & y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
0
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 xx and yy. 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 zz. The value of zz is then printed to the console.

Bitwise OR

|| (Bitwise OR) Operator

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)
operators.py
# Bitwise OR operator
x = 10
y = 5
z = x | y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
15
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 xx and yy. 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 zz. The value of zz is then printed to the console.

Bitwise XOR

^^ (Bitwise XOR) Operator

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)
operators.py
# Bitwise XOR operator
x = 10
y = 5
z = x ^ y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
15
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 xx and yy. 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 zz. The value of zz is then printed to the console.

Bitwise NOT

~~ (Bitwise NOT) Operator

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)
operators.py
# Bitwise NOT operator
x = 10
z = ~x
print(z)

Output:

command
C:\Users\Your Name> python operators.py
-11
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 xx. 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 zz. The value of zz is then printed to the console.

Bitwise Left Shift

<<<< (Bitwise Left Shift) Operator

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)
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
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 xx. 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 zz. The value of zz is then printed to the console.

Bitwise Right Shift

>>>> (Bitwise Right Shift) Operator

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)
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
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 xx. 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 zz. The value of zz is then printed to the console.

Conclusion

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!

Was this page helpful?

Let us know how we did