Bitwise Operators in Python

Sometimes we work with unusual digits or values which are involved in our program’s operations. One such case is when we work with Binary digits(bits) of integer values. Bitwise Operators in Python allow us to work or manipulate such data when required in our program. The Python language supports working with binary digits (bits) of integer values, where each bit of a number is considered separately. To achieve this, Python uses so-called bitwise or bitwise operators, which implement well-known bitwise operations. There is also support for bitwise operators in other programming languages.

Utilizing Bitwise Operations in Python

In bitwise operators (operations), each operand is treated as a sequence of binary digits (bits) that take the value 0 or 1 (binary system). Well-known operations can be performed on these bits (logical “AND”, logical “OR”, etc.)

The list of Bitwise Operators in Python in descending order of precedence is as follows:

  • ~ – bitwise operator NO (inversion, highest priority);
  • << , >> – operators of left shift or right shift by a given number of bits;
  • & – bitwise AND operator;
  • ^ – bitwise exclusive OR (XOR);
  • – bitwise OR operator (OR).

Bitwise operator inversion (~)

In the bit operator (operation) ~ inversion, the value of any bit of a number is reversed. Bit 0 is set to 1 and 1 is set to 0. That is, a positive number becomes negative with an offset of -1. Also, a negative number becomes positive with an offset of -1.

a = 10
b = 4
 
# Print bitwise AND operation
print("a & b =", a & b)
 
# Print bitwise OR operation
print("a | b =", a | b)
 
# Print bitwise NOT operation
print("~a =", ~a)
 
# print bitwise XOR operation
print("a ^ b =", a ^ b)
Output:
a & b = 0
a | b = 14
~a = -11
a ^ b = 14

Operators of left shift (<<) , right (>>)

The left shift << and right shift >> operators shift each bit one or more positions to the left or right. The general form of operators is as follows

op1 << op2
op1 >> op2
# Shift left << and shift right >> 
x = 5 # shift left by 3 digits, multiply by 2 ** 3 = 8 
y = x << 3 # y = x * 2 ** 3 = 40 
print ( 'x = ' , x)
 print ( ' y = x << 3 = ' , y)

x = 25
y = x >> 2 # y = 6
print('x = ', x)
print('y = x>>2 = ', y)

# For negative numbers
x = -10
y = x << 1 # y = -20
print('x = ', x)
print('y = x<<1 = ', y)

x = -100
y = x >> 3 # y = -13
print('x = ', x)
print('y = x>>3 = ', y)

where op1 , op2 are operands. The operand can be a number, an integer variable, or an expression that returns an integer result.

Output:
x = 5
y = x<<3 = 40
x = 25
y = x>>2 = 6
x = -10
y = x<<1 = -20
x = -100
y = x>>3 = -13

Bitwise operator AND (&)

The bitwise AND operator is binary and performs a bitwise “AND” for each pair of bits of the operands that are located to the left and right of the & operator sign . The general form of the operator is as follows

op1 & op2

where op1 , op2 are operands. Operands can be numbers, integer variables, or expressions that return an integer result. Each integer operand is treated as a set of bits, on any of which a bitwise AND operation is performed.

# Bit operation & (AND, AND)
x = 37
y = 58
z = x & y # z = 32
print('x = ', x)
print('y = ', y)
print('z = ', z)
Output:
x =  37
y =  58
z =  32

Bitwise operator exclusive OR, XOR (^)

The bitwise exclusive OR operator is denoted by ^ and performs modulo-2 addition on any bit of the operands. The exclusive OR (XOR) operator operates in binary digits. Each operand is treated as a sequence of bits. The result of a bitwise exclusive OR is determined by the following formulas:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
# Bitwise operator - exclusive OR (XOR)
x = 37
y = 58
z = x ^ y # z = 31 
print ( 'x =' , x)
 print ( 'y =' , y)
 print ( 'z =' , z)
Output:
x = 37 
y = 58 
z = 63

Bitwise operator OR (|)

The bitwise OR operator (OR) is binary and is denoted by the symbol (|). The operator implements bitwise logical addition similar to the & and ^ operators.

# Bitwise OR (OR)
x = 37
y = 58
z = x | y # z = 63 
print ( 'x =' , x)
 print ( 'y =' , y)
 print ( 'z =' , z)
Output:
x = 37
y = 58
z = 63

Examples of using bitwise operators

# Draw out 4,5,6 bits of an integer 
# enter an integer 
number = int ( input ( 'Input number:' ))

# filter for 4,5,6 bits
number & = 0b1110000

# shift 4 digits to the right
number >> = 4
print ( 'number =' , number)
Output:
Input number: 95 
number = 5

Thus, we have gone through the concepts of Bitwise operators in Python along with various examples. In the next article we will explore the concepts and examples of Membership Test Operators.

Leave a Comment