What are Bitwise operators in Python?

bitwise operators in python

Bitwise operators in python are characters or symbols that are used to manipulate the individual bits in a number or a given piece of data.

At the basic or machine level, computers process and store information in streams of 0s and 1s. Hence, every piece of text or data in the computer is internally represented in binaries.

Though as a programmer, you don’t have to worry about the intricacies of data encoding or decoding, there are times when it becomes pertinent.

Bitwise operators in Python

Any operation that involves the use or manipulation of data in its binary form is known as a bitwise operation.  Since these operations are performed at the binary level, they are executed faster by the computer.

For this, bitwise operations are widely used in encryptions, communications over networks, graphics and much more.

In Python, bitwise operations are done using the bitwise operators. With these operators, you can perform all sorts of data manipulation on data at the basic or binary level.

The followings are the bitwise operators in python and their meanings.

Operator Meaning
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
^ XOR
<< Bitwise left shift
>> Bitwise right shift

Bitwise AND operator

This operator is represented with the symbol &. For each given position in the operands, if the corresponding bits have the value of 1, the outcome for the position is 1. Contrary to this, the outcome is 0.

You can also determine the outcome of the bitwise AND operation at any given position using the formula:

(A & B)i = Ai x Bi

This is demonstrated in the figure below, where the respective bits in a given position are multiplied.

bitwise and operator

 

For better understanding, the following code expressed numbers in binary forms and outcomes are converted to binary.

#bitwise and

num1 = 0b1101
num2 = 0b1011

result = bin(num1 & num2)
print(result)

num3 = 0b100
num4 = 0b101

result = bin(num1 & num2)
print(result)

#bitwise and on integers

num5 = 15
num6 = 5

result = num5 & num6
print(result)

output

0b1001
0b1001
5

Bitwise OR operator

This operator returns 1 if one or both operands have a binary digit of 1 in any given position. If both operands have 0 at any given position, the outcome is 0.

The bitwise operator or is represented with the symbol |.

Also, you can determine the outcome of OR bitwise operation using the formula below.

(A | B)i = Ai + Bi

bitwise or operator

In the illustration above, 1 is returned if the sum of the binary digits in a given position is 1 or greater than 1. Now, let’s further demonstrate it using the code below.

#bitwise or

num1 = 0b1101
num2 = 0b1011

result = bin(num1 | num2)
print(result)

num3 = 0b100
num4 = 0b101

result = bin(num3 | num4)
print(result)

output

0b1111
0b101

Bitwise XOR operator

The bitwise XOR operator returns 1 if the bits in a given position are opposing values. That is, if the corresponding bits in a given position are both 1 or both 0, the outcome is 0.

The bitwise operator xor is represented with the symbol ^.

You can determine the outcome of the XOR bitwise operation using the formula below.

(A | B)i = (Ai + Bi) mod 2

In the illustration below, the bits in a given position are first added. Then, the resulting value is divided by 2 and the modulus or remainder becomes the outcome.

bitwise xor operator

Below is a Python code involving the xor bitwise operator.

#bitwise operator xor

num1 = 0b1101
num2 = 0b1011

result = bin(num1 ^ num2)
print(result)

num3 = 0b110111
num4 = 0b101011

result = bin(num3 ^ num4)
print(result)

output

0b110
0b11100

NOT Bitwise Operator

Most bitwise operators work on two sets of operands with the exception of the bitwise NOT operator.  The not bitwise operator is a unary operator and is represented with the symbol ~.

It is used to perform logical negation and the outcome is determined with the formula below:

~Ai  = 1 – Ai

This is illustrated in the figure below:

bitwise not operator

More examples are provided in the code below:

[bitwise_not.py]

#bitwise not

num = 0b10
result = bin(~num)
print(result)


num = 7
result = ~num
print(result)

output

-0b11
-8

Bitwise LEFT SHIFT operator

This operator moves the bits of the first operand to the left. The second operand provides the number of moves to be made on the first operand. At the same time, an equal number of zeros are padded on the right.

The left shift bitwise operator is represented with the symbol <<.

bitwise left shift operator

Here is how it is implemented in Python.

#bitwise leftshift

num = 0b1001

result = bin(num << 1)
print(result)

result = bin(num << 2)
print(result)

result = bin(num << 3)
print(result)

output

0b10010
0b100100
0b1001000

Also, the left shift bitwise operator can be evaluated using the following formula.

A << n = Ax2n

So, 5 << 2 is the same thing as:

5×22 = 5×4 = 20

print(5 << 2)

output

20

Bitwise RIGHT SHIFT operator

The right shift bitwise operator moves the bits of the first operand to the right. The second operand provides the number of moves to be made on the first operand. It moves the bits to the right while padding zeros to the left. It is represented with the symbol >>.

bitwise right shift operator

The code below demonstrates right shift bitwise operations.

[bitwise_rightshift.py]

#bitwise right shift

num = 0b1001

result = bin(num >> 1)
print(result)

result = bin(num >> 2)
print(result)

result = bin(num >> 3)
print(result)

result = bin(num >> 4)
print(result)

output

0b100
0b10
0b1
0b0

The outcome of a right shift bitwise operation can be determined by the formula below with floor division:

A >> n = A/2n

5 >> 2 is the same thing as:

5//2×2 = 5//4 = 1

print(5 >> 2)

output

1

Shorthands with bitwise operators

Just like in arithmetic operators, you can use the bitwise operators in short forms.

For instance:


[code language="python"]
num += 2
num &= 2

Let’s look at how you can use this operator in compound forms.

Operator Compound operator
& &=
| |=
^ ^=
<< <<=
>> >>=

[compound bitwise operators code]

#shorthands for bitwise operators

#bitwise and operator
num = 0b1110
num &= 0b1011
print(bin(num))

#bitwise or operator
num = 0b1110
num |= 0b1011
print(bin(num))

#bitwise xor operator
num = 0b1110
num &= 0b1011
print(bin(num))

#bitwise leftshift operator
num = 0b1110
num <<= 1
print(bin(num))

#bitwise right shift operator
num = 0b1110
num >>= 1
print(bin(num))

output

0b1010
0b1111
0b1010
0b11100
0b111

Conclusion

As much as bitwise operations are not compulsory knowledge, no knowledge is a waste. You never can tell, you may find yourself working on projects where knowledge of bitwise operations is requisite.

Many software systems rely on bit processing, such as networking protocols, data storage and file systems, and binary file systems. If you are working on any application that deals directly with the machine, you would to be knowledgeable in bitwise operations.


Leave a Reply

Your email address will not be published. Required fields are marked *