Site icon DataFlair

Python Bitwise Operators with Syntax and Example

Python Bitwise Operators with Syntax and Example

Python Bitwise Operators with Syntax and Example

Python course with 57 real-time projects - 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’

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.

Exit mobile version