Introduction to Bitmasking in Python
This tutorial educates about bit masking in Python and demonstrates how we can perform bit masking with bitwise operators, for instance, AND
, OR
, NOT
, etc.
Introduction to Bitmask in Python
Bitmask is a general concept for almost every programming language; basically, it is just a variable that aids you with bitwise operations and helps in a deep understanding and manipulating the bits of a value, let’s say integer values.
It also helps compress the values into a smaller and more efficient version, reducing the memory usage and operations on bits. In computers, every piece of data or information is stored in bits on a lower level.
This bit can only be in two states, either 0
(False
) or 1
( True
), and a specific combination has a particular meaning.
Let’s see an example to understand the bitmask concept in Python.
# convert 8 into binary format > 1000
bindata = format(8, "b")
print(bindata)
Output:
1000
In the above example, we have converted 8
, a decimal integer, into a binary form, equivalent to the 1000
. Now, we can interact with 8
on a deeper and manipulate every bit of it.
This is where the concept of bitmasking comes into the picture. But, first, let’s see an example of how we can interact with the binary bits.
Example Code:
bin_a = int(format(8, "b")) # 1000
bin_b = int(format(1, "b")) # 0001 or 1
# 1000 (8)
# 0001 (1) OR () Bitwise Operator
# 
# 1001 (9)
print(f"bin_ a = {bin_a}")
print(f"bin_ b = {bin_b}")
print(f"The OR (SUM) of bin_a and bin_b = {(bin_a  bin_b)}")
Output:
bin_ a = 1000
bin_ b = 1
The OR (SUM) of bin_a and bin_b = 1001
In the above code fence, we have used the bitwise operator OR
represented as 
, which is used to add binary values bit by bit.
Bitwise Operators in Python
Bitwise operators perform bitbybit operations of any integral type short
, byte
, long
, char
, int
, etc. There are different bitwise operators each has a specific use case.
 Bitwise OR

 Bitwise AND
&
 Bitwise XOR
^
 Bitwise Complement
~
Bitwise OR 
The OR
operator is a bitwise operator represented as 
, and it does bitbybit manipulation of two or more values and returns true
when at least one value is true
or otherwise false
.
Following is the basic structure of OR ()
for two values:
 A
 B
 A  B

 ———  ———  ——— 
 1 (True)  1 (True)  1 (True) 
 0 (False)  0 (False)  0 (False) 
 1 (True)  0 (False)  1 (True) 
 0 (False)  1 (True)  1 (True) 
Example Code:
# Bitwise operator OR ()
a = int(format(5, "b")) # 101
b = int(format(4, "b")) # 100
# 101 (5)
# 100 (4) OR () Bitwise Operator
# 
# 101 (5)
print(f"a = {a}\nb = {b}")
print("a & b = ", (a  b)) # 101
Output:
a = 101
b = 100
a & b = 101
Bitwise AND &
The AND
operator is a bitwise operator represented as &
, and it does a bitbybit calculation of two or multiple values and returns true
if both values are true
; otherwise, false
.
The is the basic structure of AND
(&
) for two values:
A 
B 
A & B 

1 (True)  1 (True)  1 (True) 
0 (False)  0 (False)  0 (False) 
1 (True)  0 (False)  1 (False) 
0 (False)  1 (True)  0 (False) 
Example Code:
# Bitwise operator AND (&)
a = int(format(5, "b")) # 101
b = int(format(4, "b")) # 100
# 101 (5)
# 100 (4) AND (&) Bitwise Operator
# 
# 100 (4)
print(f"a = {a}\nb = {b}")
print("a & b = ", (a & b)) # 100
Output:
a = 101
b = 100
a & b = 100
Bitwise XOR ^
It is a binary operator denoted by ^
, and it returns false
when the given two values are the same, either true
or false
; otherwise, it will return true
.
The basic structure of the XOR
operator is as follows:
A 
B 
A ^ B 

1 (True)  1 (True)  0 (False) 
0 (False)  0 (False)  0 (False) 
1 (True)  0 (False)  1 (True) 
0 (False)  1 (True)  1 (True) 
Example Code:
# Bitwise operator XOR (^)
a = int(format(7, "b")) # 101
b = int(format(4, "b")) # 100
# 111 (7)
# 100 (4) XOR (^) Bitwise Operator
# 
# 011 (3)
print(f"a = {a}\nb = {b}")
print("a ^ b = ", (a ^ b)) # 011 or 11
Output:
a = 111
b = 100
a ^ b = 11
Bitwise Complement ~
The bitwise Complement (~
) is a unary operator known as the NOT
operator. The NOT
operator inverts the bit pattern of a value.
The basic structure of the NOT
operator is:
A 
~A 

1 (True)  0 (False) 
0 (False)  1 (True) 
Let’s see an example below to learn the concept of NOT
(~).
# Bitwise operator NOT (~)
a = int(format(0, "b"))
print(f"a = {a}")
print(f"The Complement of a = {~a}")
Output:
a = 0
The Complement of a = 1
Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.
LinkedIn