Python - Bitwise Operators

Python

Share
python logo

In this tutorial we will learn about Bitwise operators in Python.

We use bitwise operators in Python to work with bits i.e. 0s and 1s.

Following are the bitwise operators that we can use in Python.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR i.e. XOR
~ One's complement
<<  Shift left
>>  Shift right

Bitwise AND

Bitwise AND & will give 1 only if both the operands are 1 otherwise, 0.

The truth table for bitwise AND.

A B A & B
0 0 0
0 1 0
1 0 0
1 1 1

In the following example we have two integer values 1 and 3 and we will perform bitwise AND operation and display the result.

# variables
x = 1
y = 3

# bitwise operation
result = x & y

print("result:", result)	# result: 1

Calculation of the bitwise AND operation for the above code.

# representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
& 3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)
----------------------------------------------------------------
  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)

Click here to learn how to convert numbers from decimal to binary.

Bitwise OR

Bitwise OR | will give 0 only if both the operands are 0 otherwise, 1.

The truth table for bitwise OR.

A B A | B
0 0 0
0 1 1
1 0 1
1 1 1

In the following example we have two integer values 1 and 2 and we will perform bitwise OR operation and display the result.

# variables
x = 1
y = 2

# bitwise operation
result = x | y

print("result:", result)	# result: 3

Calculation of the bitwise OR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  1 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0001 (binary)
| 2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
----------------------------------------------------------------
  3 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0011 (binary)

Bitwise XOR

Bitwise XOR ^ will give 1 for odd number of 1s otherwise, 0.

The truth table for bitwise XOR.

A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

In the following example we have two integer values 2 and 7 and we will perform bitwise XOR operation and display the result.

# variables
x = 2
y = 7

# bitwise operation
result = x ^ y

print("result:", result)	# result: 5

Calculation of the bitwise XOR operation for the above code.

// representing decimal in 32 bits (4 bytes)

  2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
^ 7 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0111 (binary)
----------------------------------------------------------------
  5 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0101 (binary)

One's complement

One's complement ~ is a unary operator and it converts 0s to 1s and 1s to 0s.

In the following example we have an integer values 2 and we will compute its one's complement.

# variable
x = 2

# bitwise operation
result = ~x

print("result:", result)	# result: -3

Calculation for the above code.

// representing decimal in 32 bits (4 bytes)

2 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
--------------------------------------------------------------
         ~2 = 1111 1111 1111 1111 1111 1111 1111 1101 (binary)
            = -3 (decimal)

We get -3 because we are working with signed integer number.

Shift Left

We use shift left << operator to shift the bits left.

In the following example we have an integer which we will left shift 1 position.

# variable
x = 4

# bitwise operation
result = x << 1

print("result:", result)	# result: 8

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 << 1 = 0000 0000 0000 0000 0000 0000 0000 1000 (binary)
            = 8 (decimal)

Shift Right

We use shift right >> operator to shift the bits right.

In the following example we have an integer which we will right shift 1 position.

# variable
x = 4

# bitwise operation
result = x >> 1

print("result:", result)	# result: 2

Calculation:

// representing decimal in 32 bits (4 bytes)

4 (decimal) = 0000 0000 0000 0000 0000 0000 0000 0100 (binary)
--------------------------------------------------------------
     4 >> 1 = 0000 0000 0000 0000 0000 0000 0000 0010 (binary)
            = 2 (decimal)
Share