e.g to explain operators:-
4+5=9, now in this example ‘4 ‘and ‘5’ are called operators.
The Python operator types are as follows:-
. Bitwise Operators:-
It acts on operands as if they were string of binary digits.
They operate bit by bit so name is bitwise.
Bitwise operator has two operands and works on it bit by bit instead of whole.
We will discuss python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operators in Python
In python programming all the decimal values are converted into sequence of bits i.e.0 and 1.
They work on bits such as shifting them left to right or converting value from 0 to 1.
Bitwise operators are same in most languages that support them.
Bitwise operator example :
Value of 2 is “10” in binary and 7 is “111”.
In the table below:-
Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Operator |
Meaning |
Example |
& |
Bitwise AND |
x& y = 0 (0000 0000) |
| |
Bitwise OR |
x | y = 14 (0000 1110) |
~ |
Bitwise NOT |
~x = -11 (1111 0101) |
^ |
Bitwise XOR |
x ^ y = 14 (0000 1110) |
>> |
Bitwise right shift |
x>> 2 = 2 (0000 0010) |
<< |
Bitwise left shift |
x<< 2 = 40 (0010 1000) |