Difference between logical and bitwise operator
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 Operator:
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 |
True | False | False | True | False | True |
True | True | True | True | False | False |
False | True | False | True | True | False |
False | False | False | False | True | True |
Logical Operator Priorities:
Operator | Priority |
Not | 1 |
== | 2 |
And | 3 |
Or | 4 |
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
Bitwise Operator:
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 |
0 | 0 | 0 | 0 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 | 0 | 1 |
0 | 1 | 0 | 1 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 0 | 0 | 0 |
Lets see the difference between logical and bitwise operator. Here we will use two variables and will see operators on both the variables.
Logical Operator:
i=0
j=3
i | j | logical i and j | logical i or j | logical not i | logical not j |
0 | 3 | 0 | 3 | False(0) | False(0) |
Bitwise Operator:
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
17>>1=8
15<<1=30 30>>2=7
30<<2=120
Refer for more details: https://wiki.python.org/moin/BitwiseOperators