Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as Operand.
In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into
binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in
Note: Python bitwise operators work only on integers.
|&||Bitwise AND||x & y|
||||Bitwise OR||x | y|
|^||Bitwise XOR||x ^ y|
|>>||Bitwise right shift||x>>|
|<<||Bitwise left shift||x<<|
Let’s understand each operator one by one.
Bitwise AND operator: Returns 1 if both the bits are 1 else 0.
a = 10 = 1010 (Binary) b = 4 = 0100 (Binary a & b = 1010 & 0100 = 0000 = 0 (Decimal)
Bitwise or operator: Returns 1 if either of the bit is 1 else 0.
a = 10 = 1010 (Binary) b = 4 = 0100 (Binary a | b = 1010 | 0100 = 1110 = 14 (Decimal)
Bitwise not operator: Returns one’s compliement of the number.
a = 10 = 1010 (Binary) ~a = ~1010 = -(1010 + 1) = -(1011) = -11 (Decimal)
Bitwise xor operator: Returns 1 if one of the bit is 1 and other is 0 else returns false.
a = 10 = 1010 (Binary) b = 4 = 0100 (Binary a & b = 1010 ^ 0100 = 1110 = 14 (Decimal)
a & b = 0 a | b = 14 ~a = -11 a ^ b = 14
These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two.
Bitwise right shift: Shifts the bits of the number to the right and fills 0 on voids left as a result. Similar effect as of dividing the number with some power of two.
Example 1: a = 10 a >> 1 = 5 Example 2: a = -10 a >> 1 = -5
Bitwise left shift: Shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
a = 5 = 0000 0101 b = -10 = 1111 0110 a << 1 = 0000 1010 = 10 a << 2 = 0001 0100 = 20 b << 1 = 0000 1010 = -20 b << 2 = 0001 0100 = -40
a >> 1 = 5 b >> 1 = -5 a << 1 = 10 b << 1 = -20
Bitwise Operator Overloading
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator
+ is used to add two integers as well as join two strings and merge two lists. It is achievable because ‘
+’ operator is overloaded by
int class and
str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.
Below is a simple example of Bitwise operator overloading.
And operator overloaded 8 Or operator overloaded 14 Xor operator overloaded 8 lshift operator overloaded 40960 rshift operator overloaded 8 Invert operator overloaded -11
Note: To know more about operator overloading click here.
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Python Operators
- Python - Bitwise AND of List
- Python | Operators | Question 1
- Python | Operators | Question 2
- Python | Operators | Question 3
- Python | Operators | Question 4
- Division Operators in Python
- Python | Bitwise OR among List elements
- Python Membership and Identity Operators | in, not in, is, is not
- Increment and Decrement Operators in Python
- Inplace vs Standard Operators in Python
- Logical Operators on String in Python
- Chaining comparison operators in Python
- Python Logical Operators with Examples
Improved By : andreasschlapbach