Bitwise Operators In Python And Their Application In Logic Gates
We use operators to perform an action on variables and values. In Python, operators can perform both logical and arithmetic operations on values which are called operands. These are used in conditional and logical statements to test a condition and then perform a routine.
Types Of Operators
The following are bitwise operators used in Python. They are called bitwise because they require conversion to binary bits from an integer number format. Operations are performed at the machine readable bit level, while the result is returned in human readable decimal format.
Bitwise AND
The operator symbol for AND is &. The statement is true (1) if the value of x and y are 1. Both values must be equal to 1. If only one variable is 1, the result is false (0).
x = 1
y = 1x & y = True (1)x = 0
y = 1x & y = False (0)x = 0
y = 0x & y = False (0)
Bitwise OR
The operator symbol for OR is |. The statement is true (1) if either value of x or y are 1. Both values must be equal to 0 for the result to be 0. If both values are set to 1, the result is true (1). If both values are set to 0, the result is false (0).
x = 1
y = 0x | y = True (1)x = 1
y = 1x | y = True (1)x = 0
y = 0x | y = False (0)
Bitwise NOT
The operator symbol for NOT is ~. This is a negation operator, meaning it is the opposite of the value. If x = 1, then ~x = 0. If y = 0, then ~y = 1. ~x can be read as NOT x, while ~y can be read as NOT y.
x = 1, ~x = 0
y = 0, ~y = 1x = 0, ~x = 1
y = 1, ~y = 0
Bitwise XOR
The operator symbol for XOR is ^. In this operation, all values returned will be false (0), unless the value of either x or y is 1. The result returns true (1) if x = 1 and y = 0 (vice versa). If both values are 1, then the result is 0.
x = 1, x = 0
y = 0, y = 1x ^ y = True (1)x = 1
y = 1x ^ y = False (0)x = 0
y = 0x ^ y = False (0)
Bitwise Shifting
Bitwise shift operators move or shift the position of bits, either to the left or to the right. Shifting to the right >> is the same as dividing a number. Shifting to the left << is the same as multiplying a number.
x = 10Bitwise Shift Right
x >> 1 = 5
x >> 2 = 2Bitwise Shift Left
x << 1 = 20
x << 2 = 40
It is better to understand this from the machine level. Let us look at it from the binary perspective in bits.
x = 10In Binary10 = 1010Bitwise Shift Right by 1x >> 1 = 51010 -> 0110
In Python, if you try to further bitwise shift (x =10) to the right by 4 or greater, it will return 0.
x = 10x >> 4 = 0
x >> 5 = 0
x >> 6 = 0
It cannot be shifted further beyond 0, that would result in a negative value. In this case it is not within that scope.
A Simple Logic Circuit
The following is a logic circuit which uses logic gates that represent the bitwise operators AND, OR, NOT and XOR. Each operation occurs at the logic gate, represented by the operator symbol. At the end we will apply bitwise shifting to the final result of the value.
Here is the logic circuit diagram.
We will assign the following values.
A = 12
B = 10
C = 8
D = 4
E = 5
The result of the operation is the value of X.
X = ~(A & B) ^ [(C | D) ^ ~ E]
Let’s begin from the top going down with the first set of operations:
AND Gate
A & B12 & 10 = 8
OR Gate
C | D 8 | 4 = 12
NOT Gate
~ E~ 5 = -6
The next set of operations follow from the results.
NOT Gate
~ (A & B)~ 8 = -9
XOR Gate
(C | D) ^ ~ E12 ^ -6 = -10
Finally, we reach the last operation to get the value of X.
XOR Gate
~(A & B) ^ [(C | D) ^ ~ E]-9 ^ -10 = 1
The value of X is equal to 1.
Now we can bitwise shift it to the left or the right by 1.
1 >> 1 = 0
1 << 1 = 2
Here is the full Python program:
A = 12
B = 10
C = 8
D = 4
E = 5firstGate = A & B
print("The value of the first gate is: ", firstGate)secondGate = C | D
print("The value of the second gate is: ", secondGate)thirdGate = ~ E
print("The value of the third gate is: ", thirdGate)fourthGate = ~ (A & B)
print("The value of the fourth gate is: ", fourthGate)fifthGate = (C | D) ^ ~ E
print("The value of the fifth gate is: ", fifthGate)X = fourthGate ^ fifthGate
print("The value of X is: ", X)
The output will show the following:
The value of the first gate is: 8The value of the second gate is: 12The value of the third gate is: -6The value of the fourth gate is: -9The value of the fifth gate is: -10The value of X is: 1
In our circuit example, the values can represent the data that is being processed. This eventually leads to the final result.