Hi Friends, welcome to yet another session in our Python Tutorial Series. Today we will see the Difference between logical and bitwise operator in Python. We have already seen mathematical operators in Python in another session:
Lets see below logical and bitwise operators one by one:
Logical operators return the logical output of the expression on which the operator is applied. The answer can be either True/False or 1/0. Below are the logical operators:
and operator – This operator is applied on two conditions and return true when both the conditions are true
or operator – This operator is applied on two conditions and returns true when any one of the conditions is true
not/negation operator – it just negates the condition. This operator is applied on only one condition
This is how the operators behave when used in code:
print(0 and 1) – returns 0
print(0 or 1) – returns 1
print(not 0) – returns True
print(not 2) – returns False
Logical Operators Behavior:
|Variable 1||Variable 2||And||or||not Variable1||not Variable2|
Logical Operator Priorities:
Some examples of logical expressions:
print(10>3) – return True
print(5<2) – return False
print(2!=0) – return True
print(not(2==2)) – return False
Apart from logical operators, we also have bitwise operators. As the name suggest, bitwise operator works on each bit of the two variables while logical operator just worry the value of the two variables. For example, if we have two variables as 1011 and 0101, then bitwise operators will check these two variables at each bit level. Hence there will be 4 checks – 0th bit of both the operators will he checked, 1st bit of both the operators will be checked and so on. Below are the bitwise operators:
&/bitwise and – Applied on two bits and return True when both the bits are True
|/bitwise or – Applied on two bits and returns True when any one of the bits is True
~/bitwise negation – Applied on one bit and negates the bit.
^/xor – Applied on two bits and return True when exactly one of the bit is True
Bitwise Operators Behavior:
|Variable1||Variable2||&|||||^||~ Variable1||~ Variable2|
Lets see the difference between logical and bitwise operator. Here we will use two variables and will see operators on both the variables.
|i||j||logical i and j||logical i or j||logical not i||logical not j|
i=5 = 101
j=6 = 110
|i||j||logical I & j||bitwise i | j||bitwise i ^ j||bitwise ~ i||bitwise ~ j|
|101||110||100 (4)||111 (7)||011 (3)||010 (2)||001 (1)|
Binary shift of numbers:
Binary shift works just like we do normal multiplication and division on integers.
For ex: 23 * 10 = 230 – this means that we shift 23 to the left by one place and put a zero in the space created
400 % 10 = 20 – this means that we shifted 400 to the right by one place and hence last zero got omitted.
In the same way binary shifting is performed. The only difference is that multiplying and dividing is done by 2 instead of 10 because binary numbers are raised to the power of 2. Decimal numbers are raised to the power of 10.
Binary left shift by one bit(var << 1)– this means that multiplying the number by 2 Binary right shift by one bit(var>>1)- this means that dividing the number by 2
Refer for more details: https://wiki.python.org/moin/BitwiseOperators