Python 3 – Learn it quick – Logical and Bitwise Operators
In python, there are mainly, three boolean operators and seven bitwise operators
Operator Name | Symbol | Sample Operation | Description |
---|---|---|---|
logical AND | and | age == 23 and name == “john” | |
logical OR | or | age == 23 or name == “john” | |
logical NOT | not | age == 23 not name == “john” | |
bitwise left shift | >> | ||
bitwise right shift | << | ||
bitwise and | & | Return 1 if both of the bits are 1 or else it will return 0 | |
bitwise xor | ^ | return 1 if one of the bit is 1 and other one is 0 or else it will return false | |
bitwise or | | | Return 1 if either of the bit is 1 or else 0 | |
bitwise not | ~ | Return its one’s complement |
logical AND
The logical AND operator validate two conditions. If both conditions are True, then it will return True or it will be False.
Lab Excercise
name = "Isaac"
year = 2
if name == "Isaac" and year == 2:
print("Student name is Isaac, and Isaac studies in year 2.")
logical OR
The logical AND operator validate two conditions. If one of the condition is True, then it will return True.
year = 3
if year == 3 or year == 2:
print("Isaac studies in year 2 or 3 ")
logical NOT
Lab Excercise
year = 3
if not year:
print("Year is empty")
else:
print("Year is ",year)
Bitwise OR
The Bitwise OR operator will return the value 1 if either of the bit is 1 or else it will return 0
Example :
a = 12, Binary of 12 is 1100 b = 14, Binary of 14 is 1110 1100 1110 a | b = 1110 Result in binary 1110 Result in decimal 14 Result in hexadecimal e
Bit | Value |
---|---|
0|0 | 0 |
0|1 | 1 |
1|0 | 1 |
1|1 | 1 |
Lab Excercise
i = 12
j = 14
# Print bitwise OR operation
print("i | j =", i | j)
Bitwise AND
The Bitwise AND operator will return the value 1 if both of the bits are 1 or else it will return 0
Example :
a = 12, Binary of 12 is 1100 b = 14, Binary of 14 is 1110 1100 1110 a & b = 1100 Result in binary 1100 Result in decimal 12 Result in hexadecimal c
Bit | Value |
---|---|
0|0 | 0 |
0|1 | 0 |
1|0 | 0 |
1|1 | 1 |
Lab Excercise
i = 12
j = 14
# Print bitwise AND operation
print("i & j =", i & j)
Bitwise XOR
Bitwise XOR will return 1 if one of the bit is 1 and other one is 0 or else it will return false
a = 12, Binary of 12 is 1100 b = 14, Binary of 14 is 1110 1100 1110 a ^ b = 0010 Result in binary 0010 Result in decimal 2 Result in hexadecimal 2
Lab Excercise
i = 12
j = 14
# Print bitwise XOR operation
print("i ^ j =", i ^ j)
Bitwise NOT
Bitwise NOT will return its one’s complement. So, it flips the bits until it reaches the first 0 from right. ~a is the same as -a-1.
a = 12, Binary of 12 is 1100 1100 ~a = ~1100 = -(1100 + 1) = -(1101) = -13, Decimal Equivalent
It can also be calculated by,
~a = -12-1 = binary(-13) = -(1101)
Lab Excercise
i = 12
# Print bitwise NOT operation
print("~i = ", ~i)
Bitwise Right Shift
Right shift operator, shifts the bits of the number to the right by the number of bits mentioned by the left operand and it will fills 0 on empty slots remained as a result of this change. It is similar to dividing the number with a power of two.
Example : If a = 12 and bit shift value is 2, then result will be, 12 /(2 ^ 2) = 12 / 4 = 3
a = 12, Binary of 12 is 1100 1100 a >> 2 = 0011 Result in binary 0011 Result in decimal 3 Result in hexadecimal 3
Lab Excercise
i = 12
# Print Bitwise Right Shift operation
print("i >> 2 =", i >> 2)
Bitwise Left Shift
The Left shift operator, shifts the bits of the number to the left by the number of bits mentioned by the right operand and it will fills 0 on empty slots remained as a result of this change. It is similar to multiplying the number with a power of two.
Example :
If the number is 12 and shift bit is 2, then result is 12 * 2^2 = 48
a = 12, Binary of 12 is 1100 1100 a << 2 = 110000 Result in binary 110000 Result in decimal 48 Result in hexadecimal 30
Lab Excercise