# Python Bitwise Operators with Syntax and Example

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this Python Bitwise Operators Tutorial, we will discuss Python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operators in Python Programming.

Along with this, we will discuss syntax and examples of Python Bitwise Operators.

So, let’s start the Python Bitwise Operators Tutorial.

Python Bitwise Operators with Syntax and Example

## What is Python Bitwise Operators?

Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole.

To take an example, letâ€™s see the â€˜andâ€™ and â€˜&â€™ operators for the same thing.

Letâ€™s take two numbers- 5 and 7. Weâ€™ll show you their binary equivalents using the function bin().

`>>> bin(5)`

Output

‘0b101’
`>>> bin(7)`

Output

‘0b111’

Technology is evolving rapidly!

Now letâ€™s try applying â€˜andâ€™ and â€˜&â€™ to 5 and 7.

`>>> 5 and 7`

Output

7
`>>> 5&7`

Output

5

You would have expected them to return the same thing, but theyâ€™re not the same. One acts on the whole value, and one acts on each bit at once.

Actually, â€˜andâ€™ sees the value on the left. If it has a True Boolean value, it returns whatever value is on the right.

Otherwise, it returns False. So, here, 5 and 7 is the same as True and 7. Hence, it returns 7.

However, 5&7 is the same as 101&111. This results in 101, which is binary for 5. Letâ€™s look at each of these operators bit by bit (pun intended).

Let’s move ahead with next Python Bitwise Operator

### 1. Python Bitwise AND (&) Operator

1 has a Boolean value of True, and 0 has that of False. Take a look at the following code.

`>>> True/2`

Output

0.5
`>>> False*2`

Output

0

This proves something. Now, the binary and (&) takes two values and performs an AND-ing on each pair of bits.

Letâ€™s take an example.

`>>> 4 & 8`

Binary for 4 is 0100, and that for 8 is 1000. So when we AND the corresponding bits, it gives us 0000, which is binary for 0. Hence, the output.

The following are the values when &-ing 0 and 1.

#### Python Bitwise Operators – AND Operators

 0 & 0 0 0 & 1 0 1 & 0 0 1 & 1 1

As you can see, an &-ing returns 1 only if both bits are 1.

You cannot, however, & strings.

`>>> '\$'&'%'`

Output

Traceback (most recent call last):File “<pyshell#30>”, line 1, in <module>’\$’&’%’

TypeError: unsupported operand type(s) for &: ‘str’ and ‘str’

Since Boolean values True and False have equivalent integer values of 1 and 0, we can & them.

`>>> False&True`

Output

False
`>>> True&True`

Output

True

Letâ€™s try a few more combinations.

`>>> 1&True`

Output

1
`>>> 1.0&1.0`

Output

Traceback (most recent call last):File “<pyshell#36>”, line 1, in <module>1.0&1.0

TypeError: unsupported operand type(s) for &: ‘float’ and ‘float’

You can also type your numbers directly in binary, as we discussed in section 6a in our Python Numbers tutorial.

`>>> 0b110 & 0b101`

Output

4

Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.

### 2. Python Bitwise OR (|) Operators

Now let’s discuss Python Bitwise OR (|) Operator

Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.

#### Python Bitwise Operators – OR Operators

 0|0 0 0|1 1 1|0 1 1|1 1
`>>> 6|1`

Output

7

This is the same as the following.

`>>> 0b110|0b001`

Output

7

Letâ€™s see some more examples.

`>>> True|False`

Output

True

Let’s move to another Python Bitwise Operator

### 3. Python Bitwise XOR (^) Operator

XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.

#### Python Bitwise Operators – XOR Operators

 0^0 0 0^1 1 1^0 1 1^1 0

Letâ€™s take a few examples.

`>>> 6^6`

Here, this is the same as 0b110^0b110. This results in 0b000, which is binary for 0.

`>>> 6^0`

Output

6

This is equivalent to 0b110^0b000, which gives us 0b110. This is binary for 6.

`>>> 6^3`

Output

5

Here, 0b110^0b011 gives us 0b101, which is binary for 5.

Now let’s discuss Bitwise 1’s Complement (~)

### 4. Python Bitwise 1â€™s Complement (~)

This one is a bit different from what weâ€™ve studied so far. This operator takes a numberâ€™s binary, and returns its oneâ€™s complement.

For this, it flips the bits until it reaches the first 0 from right. ~x is the same as -x-1.

`>>> ~2`

Output

-3
`>>> bin(2)`

Output

‘0b10’
`>>> bin(-3)`

Output

‘-0b11’

To make it clear, we mention the binary values of both. Another example follows.

`>>> ~45`

Output

-46
`>>> bin(45)`

Output

‘0b101101’
`>>> bin(-46)`

Output

‘-0b101110’

### 5. Python Bitwise Left-Shift Operator (<<)

Finally, we arrive at left-shift and right-shift operators. The left-shift operator shifts the bits of the number by the specified number of places.

This means it adds 0s to the empty least-significant places now. Letâ€™s begin with an unusual example.

`>>> True<<2`

Output

4

Here, True has an equivalent integer value of 1. If we shift it by two places to the left, we get 100. This is binary for 4.

Now letâ€™s do it on integers.

`>>> 2<<1`

Output

4

10 shifted by one place to the left gives us 100, which is, again, 4.

`>>> 3<<2`

Output

12

Now, 11 shifted to the left by two places gives us 1100, which is binary for 12.

Now let’s move to Next Python Bitwise Operator

### 6. Python Bitwise Right-Shift Operator (>>)

Now weâ€™ll see the same thing for right-shift. It shifts the bits to the right by the specified number of places.

This means that those many bits are lost now.

`>>> 3>>1`

Output

1

3 has a binary value of 11, which shifted one place to the right returns 1. But before closing on this tutorial, weâ€™ll take one last example.

Letâ€™s check whatâ€™s the decimal value for 11111.

`>>> int(0b11111)`

Output

31

Now, letâ€™s shift it three places to the right.

`>>> 31>>3`

Output

3

As you can see, it gives us 3, whose binary is 11. Makes sense, doesnâ€™t it?

This was all about the Python Bitwise Operators.

### Python Interview Questions on Bitwise Operators

1. What is the purpose of Python Bitwise Operator?
2. Explain Python Bitwise Operator with an example?
3. Are Python Bitwise Operators faster?
4. What is Bitwise Not Operator?
5. Where are Python Bitwise Operators used?

## Conclusion

While theyâ€™re not so common in real-world programming, Python Bitwise Operators do find their use in places like encryption, compression, and byte manipulation.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

### 9 Responses

1. Todd says:

Thanks, very good. My issue using bitwise in Python is understanding what types can or should be used – is an int assumed? Is that 4 bytes? I get errors when trying to use byte types in python

2. Jaydeep Kandiya says:

i don’t understand how 7 comes when in and 5 and 7

• Jaydeep Kandiya says:

sorry i didn’t read below info…

3. Msk says:

Thank you so much for such great resource.
I used to work on embedded project where we used python a lot to manipulate the bits.
I found, that using bitwise operations, sometimes you code could be mode compact even though for me it takes some time to understand. Let say i can convert the bits to string then change the way i want then convert back (this simple example) which is not optimal.
I am wondering is there any set of standardized shortcuts (bitwise tricks, bitwise coockbook) that developers are using, that i can learn and used in future or it comes with experience?
I am sorry cannot give you exact example of code.

4. Msk says:

sorry was not able to edit my comment..
I found this code from stackoverflow.
how user named(travc) comes up with this idea (twos complement)
def twos_comp(val, bits):
“””compute the 2’s complement of int value val”””
if (val & (1 << (bits – 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val – (1 << bits) # compute negative value
return val # return positive value as is

• DataFlair says:

Two’s complement subtracts off (1<<bits) if the highest bit is 1 otherwise it doesn't do anything. That was the idea behind this approch.

How to find bitwise AND of two binary numbers without using the logical symbols ‘&’. Please help me with this problem

• DataFlair says:

You can iterate over all the bits in the number from left to right or right to left and put if else ladder or switch case. Eg:
num1=1
num2=0
if(num1==1 and num2== 1):
ans = 1
else :
ans=0
print(ans)

6. Daniel Ugochukwu Onovo says:

Simple explanation, I would really appreciate a practical implementation of the bitwise operators in real life problems