Page Contents
Python Bitwise operators works bit by bit. Python bitwise operators first convert integers into a binary number and then work on those binary number bit by bit . After the operation it converts the new binary number into a decimal number and returns the decimal number .Below is a list of all Python bitwise operators and their applications –
The values of the variables x
and y
used in the table are 6
and 10
respectively whose binary values are 0110
and 1010
respectively.
Operator(name) | Example | Description | Result |
---|---|---|---|
& (Bitwise AND) | x & y | Set each bit to 1 if both bits are 1 else 0 | 6 & 10 = 0110 & 1010 = 0010 = 2 |
| (Bitwise OR) | x | y | Set each bit to 1 if any of the bits is 1 else 0 | 6 | 10 = 0110 | 1010 = 1110 = 14 |
~ (Bitwise NOT or one’s complement) | ~ x | Returns -(x +1) | ~ 6 = ~ 0110 = -(0110+1) = -0111 = -7 , ~10 = ~1010 = -(1010+1) = -1011 = -11 |
^ (Bitwise XOR) | x ^ y | Set each bit to 1 if only one of two bits is 1 else 0 | 6 ^ 10 = 0110 ^ 1010 = 1100 = 12 |
<< (Bitwise Left Shift) | x << 2 | Shift left by pushing zeros at the right side | 6 << 2 = 0110 << 2 = 011000 = 24 ,10 << 2 = 1010 <<2 = 101000 = 40 |
>> (Bitwise Right Shift) | x >> 2 | Shift right by removing bits from right side | 6 >> 2 = 0110 >> 2 = 01 = 1 ,10 >> 2 = 1010 >> 2 = 10 = 2 |
Now we will see about all the logical operators in details with code –
Bitwise AND(&) operator
Python Bitwise AND(&
) operator sets each bit to 1 if both the bits are 1 else 0 .
Example 1:
x=6
y=10
print("x & y =",x & y)
x & y = 2
In the above code value of x
and y
are 6
and 10
respectively . During bitwise AND(&
) operation it converts the value of x
and y
into 0110
and 1010
respectively and returns a binary number 0010
whose decimal equivalent is 2
. we can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-and.png?resize=429%2C300&ssl=1)
We can also refer the below table for bitwise AND(&
) operator –
Variable1 | Variable2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Bitwise OR(|) operator
Python Bitwise OR(|
) operator sets each bit to 1 if any of the bits is 1 else 0
Example 1:
print("x | y =",x | y)
x | y = 14
In the above code bitwise OR(|
) operators returns 14
. We can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-or.png?resize=441%2C294&ssl=1)
We can also refer the below table for bitwise OR(|
) operator –
Variable1 | Variable2 | Result |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Bitwise NOT(~) operator
Python Bitwise NOT(~
) operator returns -(x+1) if we give x after bitwise NOT(~
) operator .
Example 1:
print("~ x =",~ x)
print("~ y =",~ y)
~ x = -7 ~ y = -11
In the above code ~x
returns -7
and ~y
returns -11
. We can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-not.png?resize=547%2C284&ssl=1)
Bitwise XOR(^) operator
Bitwise XOR(^
) operator sets each bit to 1 if only one of two bits is 1 else 0
Example 1:
print("x ^ y =",x ^ y)
x ^ y = 12
In the above code bitwise XOR(^
) operator returns 12
. We can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-xor.png?resize=420%2C294&ssl=1)
We can also refer the below table for bitwise XOR(^
) operator –
Variable1 | Variable2 | Result |
---|---|---|
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Bitwise Left Shift(<<) operator
Python Bitwise Left Shift(<<
) operator shifts each bit at left by pushing zeros at the right side
Example 1:
x=13
print("x << 1 =",x << 1)
print("x << 2 =",x << 2)
x << 1 = 26 x << 2 = 52
In the above code we take the value of x
as 13
whose binary equivalent is 1101
. Here , x<<1
returns 26
and x<<2
returns 52
. We can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-left-shift.png?resize=783%2C340&ssl=1)
Note :
We can see from the above example whenever we Left Shift(<<
) any number by 1 place the returned number is equal to 2 times of the given number .
Bitwise Right Shift(>>) operator
Python Bitwise Right Shift(>>
) operator shifts each bit at right by removing the rightmost bit .
Example 1:
print("x >> 1 =",x >> 1)
print("x >> 2 =",x >> 2)
x >> 1 = 6 x >> 2 = 3
In the above code x>>1
returns 6
and x>>2
returns 3
. We can see the following image as reference –
![](https://i0.wp.com/www.codingconception.com/wp-content/uploads/2022/01/bitwise-right-shift.png?resize=797%2C372&ssl=1)
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .
Helpful info. Fortunate me I discovered your site by chance, and I am surprised why this twist of fate did not came about earlier!
I bookmarked it.
Feel free to surf to my webpage – hp888 เครดิตฟรี
May I simply just say what a comfort to discover somebody who truly understands
what they’re talking about on the web. You definitely realize how to bring a problem to light
and make it important. More and more people ought to check this
out and understand this side of the story. It’s surprising you are not more
popular because you definitely possess the gift.
Feel free to visit my site – ufa1669 เครดิต ฟรี
Hello to all, how is all, I think every one is getting
more from this site, and your views are nice in favor of new
viewers.
my website … grand lisboa คาสิโนออนไลน์