Bitwise Operators
These operators act on individual bits (0 and 1) of the
operands.
We can use bitwise operators directly on binary
numbers or on integers also.
When we use these operators on integers, these
numbers are converted into bits (binary number
system) and then bitwise operators act upon those bits.
The results given by these operators are always in the
form of integers.
We use decimal number system in our daily life. This
number system consists of 10 digits from 0 to 9. We
count all numbers using these 10 digits only.
But in case of binary number system that is used by
computers internally, there are only 2 digits, i.e. 0 and
1 which are called bits (binary digits). All values are
represented only using these two bits. It is possible to
convert a decimal number into binary number and vice
versa.
Example 1: Converting 45 into binary number system.
Rule: Divide the number successively by 2 and take
the remainders from bottom to top, as shown in Figure
below.
The decimal number 45 is represented as 101101 in
binary.
If we use 8 bit representation, we can write it as:
0010 1101.
Converting into Binary
Example 2: Converting binary number 0010 1101 into
decimal number.
Rule: Multiply the individual bits by the powers of 2
and take the sum of the products, as shown in Figure
below.
Converting from Binary to Decimal
Here, the sum is coming to 45.
So 0010 1101 in binary is equal to 45 in decimal
number system.
There are 6 types of bitwise operators as shown
below:
Bitwise Complement operator ( ~ )
Bitwise AND operator (&)
Bitwise OR operator (|)
Bitwise XOR operator (^)
Bitwise Left shift operator (<<)
Bitwise Right shift operator (>>)
Bitwise Complement Operator ( ~ )
This operator gives the complement form of a given
number. This operator symbol is ~, which is
pronounced as tilde.
Complement form of a positive number can be obtained
by changing 0’s as 1’s and vice versa.
The complement operation is performed by NOT gate
circuit in electronics. Truth table is a table that gives
relationship between the inputs and the output. The
truth table is also given for NOT gate, as shown in
Figure below:
NOT gate that Performs Complement of Bits
If x = 10, find the ~x value.
x = 10 = 0000 1010.
By changing 0’s as 1’s and vice versa,
we get 1111 0101.
This is nothing but -11(in decimal).
So, ~x = -11.
Step-by-Step Breakdown
Apply the formula: Substitute 10 for x.
Calculate: -(10 + 1) = -11
Binary Representation (using 8-bit Two's Complement)
10 in binary: 00001010
Invert the bits (~10): 11110101
Interpret 11110101 in Two's Complement:
The leading 1 means it is a negative number.
Subtracting 1 gives 11110100
and flipping the bits back gives 00001011, which
represents -11.
Bitwise AND Operator (&)
This operator performs AND operation on the individual
bits of numbers. The symbol for this operator is &,
which is called ampersand. To understand the bitwise
AND operation, see the truth table given in Figure
below:
From the truth table, we can conclude that by
multiplying the input bits, we can get the output bit.
The AND gate circuit present in the computer chip will
perform the AND operation.
If x = 10, y = 11. Find the value of x&y.
x = 10 = 0000 1010.
y = 11 = 0000 1011.
From the truth table, by multiplying the bits, we can get
x&y = 0000 1010.
This is nothing but 10 (in decimal).
Bitwise OR Operator ( | )
This operator performs OR operation on the bits of the
numbers.
The symbol of bitwise OR operator is |, which is called
pipe symbol.
To understand this operation, see the truth table given
in Figure below.
From the table, we can conclude that by adding the
input bits, we can get the output bit. The OR gate
circuit, which is present in the computer chip will
perform the OR operation.
If x = 10, y = 11, find the value of x|y.
x = 10 = 0000 1010.
y = 11 = 0000 1011.
The truth table shows that by adding the bits, we can
get x|y = 0000 1011.
This is nothing but 11 (in decimal).
Bitwise XOR Operator ( ^ )
This operator performs exclusive or (XOR) operation on
the bits of numbers.
The symbol is ^, which is called cap, carat, or
circumflex symbol.
To understand the XOR operation, see the truth table
given in Figure below.
From the table, we can conclude that when we have
odd number of 1’s in the input bits, we can get the
output bit as 1.
The XOR gate circuit of the computer chip will perform
this operation.
If x = 10, y = 11, find the value of x^y.
x = 10 = 0000 1010.
y = 11 = 0000 1011.
From the truth table, when odd number of 1’s are
there, we can get a 1 in the output.
Thus, x^y = 0000 0001 is nothing but 1 (in decimal).
Bitwise Left Shift Operator (<<)
This operator shifts the bits of the number towards left
a specified number of positions. The symbol for this
operator is <<, read as double less than.
If we write x<<n, the meaning is to shift the bits of x
towards left n positions.
If x = 10, calculate x value if we write x<<2.
Shifting the value of x towards left 2 positions will make
the leftmost 2 bits to be lost.
The value of
x is 10 = 0000 1010.
Now, x<<2 will be
0010 1000 = 40 (in decimal).
The procedure to do this is explained, as shown in
Figure below:
Shifting bits towards left 2 times
Bitwise Right Shift Operator (>>)
This operator shifts the bits of the number towards
right a specified number of positions.
The symbol for this operator is >>, read as double
greater than.
If we write x>>n, the meaning is to shift the bits of x
towards right n positions.
>> shifts the bits towards right and also preserves the
sign bit, which is the leftmost bit.
Sign bit represents the sign of the number.
Sign bit 0 represents a positive number and 1
represents a negative number.
So, after performing >> operation on a positive
number, we get a positive value in the result also.
If right shifting is done on a negative number, again we
get a negative value only.
If x = 10, then calculate x>>2 value.
Shifting the value of x towards right 2 positions will
make the rightmost 2 bits to be lost.
x value is 10 = 0000 1010.
Now x>>2 will be: 0000 0010 = 2 (in decimal)
Shifting bits towards right 2 times
Let’s check the effects of various bitwise operators so
far discussed. We will use IDLE window and type the
Python statements and confirm the results, as shown in
Figure below:
Core Practical Uses
Managing Configuration Flags: Combine or check
multiple boolean settings in a single integer variable.
Optimizing Math Operations: Multiply or divide by
powers of two instantly using bit shifts.
Parsing Binary Data: Extract specific bits from
network packets, file headers, or hardware sensor
streams.
Cryptography and Hashing: Shuffle and obscure
data bits in security algorithms.
Low-Level Device Control: Turn hardware pins on or
off by modifying specific bits.