Bitwise operators in Python allow direct manipulation of data at the binary level. This enables high-performance, low-level control in your code. This operation is essential in domains where memory efficiency, speed, and minute control over the data are very important. In domains like system programming, cryptography, embedded systems, and graphical programming, bitwise operators are widely used. In this blog, you will explore what bitwise operators are, the different types available in Python, and how each one works with examples in detail.
Table of Contents:
What are Bitwise Operators in Python?
Bitwise operators in Python are symbols that instruct the interpreter of Python to perform operations at a bit level. This is known as binary manipulation. Every integer in Python is stored in Binary format. These bitwise operators are the only ones among the Python operators that let you manipulate individual bits directly.
Note: Unlike lower-level languages like C or Java, Python supports arbitrary-length integers, so you’re not limited to 8, 16, or 32 bits. You can perform bitwise operations on very large numbers without worrying about overflow.
Types of Bitwise Operators in Python
Python provides six primary bitwise operators that allow you to manipulate bits in integers. Each operator performs a different type of bit-level computation.
Operator | Name | Example | Description |
& | Bitwise AND | a & b | Sets each bit to 1 only if both bits are 1 |
| | Bitwise OR | a | b | Sets each bit to 1 if at least one bit is 1 |
^ | Bitwise XOR | a ^ b | Sets each bit to 1 only if the bits are different |
~ | Bitwise NOT | ~a | Inverts all bits (1s become 0s and 0s become 1s) |
<< | Left Shift | a << 1 | Shifts bits to the left, filling with 0s on the right |
>> | Right Shift | a >> 1 | Shifts bits to the right, discarding bits on the right |
Bitwise Logical Operators in Python
Bitwise Logical Operators compare and manipulate the bits of integers based on Boolean Logic Gates. There are four bitwise logical operators. These are AND, OR, XOR, and NOT. Let us look at each of these one by one.
Bitwise AND (&) Operator
The bitwise and operator sets each bit to 1 if and only if both corresponding bits are 1. It is represented by &.
Truth Table:
Bit A | Bit B | A & B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example:
Output:
Explanation: The binary of 10 is 1010, and for 4 is 0100. Applying the AND operation evaluates each bit using the truth table. Ignore the 0x values. Those are hexadecimal notations of the integer.
Bitwise OR Operator
The bitwise OR operator is represented by |. This operator sets the bit to one if at least one of the bits is 1.
Truth Table:
Bit A | Bit B | A | B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example:
Output:
Explanation: You can see the bit representation of 10 and 4. Based on the OR Truth Table, each bit was evaluated. Ignore the 0x values. Those are hexadecimal notations of the integer.
Bitwise XOR (^) Operator
The bitwise XOR operator is denoted by ^. This operator sets the bit to 1 if both of the bits are different, either 0 or 1, it doesn’t matter which. If both bits are the same, it will result in zero.
Truth Table:
Bit A | Bit B | A ^ B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example:
Output:
Explanation: You can see the binary representation of 10 and 4. Based on the XOR Truth Table, each bit was evaluated. Ignore the 0x values. Those are hexadecimal notations of the integer.
Bitwise NOT(~) Operator
The bitwise NOT operator is represented by ~. It inverts all the bits of a number, turning 0s into 1s and 1s into 0s. This is called taking the 1’s complement of the number.
However, in Python, when you apply ~n, the result is interpreted using two’s complement representation, which is how computers handle negative numbers internally.
In Python, ~n is equivalent to -(n + 1)
Because Python treats the inverted binary as a two’s complement negative integer.
Truth Table:
Example:
Output:
Explanation: You can see the binary representation of 10. Based on the NOT Truth Table, each bit is inverted.
Why Does ~10 Return -11?
Let us understand it step by step.
a = 10
Binary (8-bit): 00001010
Why 8 bits?
Even though 10 fits in 4 bits (1010), we use 8 bits to better visualise what’s happening; This mimics how computers typically store integers (e.g., a byte = 8 bits), and it helps us interpret the result as a signed binary number.
Apply bitwise NOT (~a) – this flips every bit
~00001010 → 11110101
Interpret the result (11110101)
This is the two’s complement representation of a negative number.
To find out which negative number:
- Invert the bits again: 00001010
- Add 1: 00001011 → 11
- So the original result must be -11
Bitwise Shift Operators in Python
The bitwise shift operators shift the bits in the binary representation of a number by a specified number of positions. In Python, we have two bitwise shift operators, Left shift and Right shift.
Left Shift (<<) Bitwise Operator
The left shift operator is represented by a << n. This operator shifts the bits in the binary representation of ‘a’ to the left by ‘n’ positions. A zero is added to the right, and the leftmost bits are discarded.
This operation is equivalent to multiplying the number by 2^n.
Example:
Output:
Explanation: Here, in 5 → 0000 0101, all the bits are shifted to the left by 1 in the first example and by 2 in the second example.
Right Shift (>>) Bitwise Operator
The right shift operator is represented by a >> n. This operator shifts the bits towards the right by n positions. Here, the rightmost bits are discarded, and the leftmost bits are filled based on the sign of the number.
- For a Positive Number, 0 is filled on the left.
- For a Negative Number, 1s are filled on the left to preserve the sign. This is called an arithmetic shift.
It performs floor division by 2^n.
Example:
Output:
Explanation: Here, 5 is represented as 0000 0101, whereas -5 is represented as 1111 1011 (due to 2s Complement). In the first example, 5 is right-shifted by 1, and in the second example, -5 is right-shifted by 1. In the negative number case, 1s were filled in from the left to preserve the sign.
Bitwise Operator Overloading in Python
In Python, the bitwise operators can be overloaded. Overloading means you can define custom behavior for bitwise operators when they are used with user-defined objects like classes.
Operator overloading allows you to change the way operators behave for your classes by defining special methods, also called magic methods. The bitwise operators are overloaded using the following methods.
Operator | Method |
& | __and__ |
| | __or__ |
^ | __xor__ |
~ | __invert__ |
<< | __lshift__ |
>> | __rshift__ |
Example:
Output:
Explanation: Here, when a & b are called, it triggers the __and__() method, which performs a bitwise AND on the values of a and b, which are 6 and 3, respectively. The result is 2, so the output is Number(2).
Conclusion
In this blog, we learned the various bitwise operators available in Python and how they work on a bit level. Python uses 2’s complement representation for negative integers, which explains the behavior of bitwise NOT and right shift operations with negative numbers. You also learned how bitwise shift operators can be used for efficient multiplication and division by powers of 2. You can even overload bitwise operators to define custom behavior for user-defined classes. Bitwise operators are widely used in access control programs. Learning and using them will improve your code.
To take your skills to the next level, check out this Python training course and gain hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
Python Bitwise Operators – FAQs
Q1. What are bitwise operators in Python?
Bitwise operators instruct the interpreter to perform operations on the binary representation of integers.
Q2. What is |= in Python?
It is a bitwise assignment OR operator.
Q3. Does Python have XOR?
Yes, Python has an XOR operator denoted by ^.
Q4. What is the '|' symbol in Python?
It is the representation of the Bitwise OR operator.
Q5. What does the |= operator do?
The bitwise assignment OR operator performs bitwise OR on the binary representation of the integers and then assigns the value to the left-hand variable.