Bitwise Operators
These operators act on
individual bits (0 and 1) of the operand. We can use Bitwise operators directly
on binary numbers or on integers also.
Bitwise Operators
|
Meaning
|
&
|
If both bits are 1 then only the result is 1 otherwise result is 0
|
|
|
If at least one bit is 1
then the result is 1 otherwise result is 0
|
^ (Cap operator)
|
It bits are different then the only result is 1 otherwise result is 0
|
~ (tilde)
|
Bitwise complement operator
i.e 1 means 0 and 0 means 1
|
>>
|
Bitwise left shift operator
|
<<
|
Bitwise right shift operator
|
Bitwise AND Operator(&):
This operator performs AND
operation on the individual bits of numbers. The symbol for this operator is ‘&’,
which is called ampersand.
The AND gate circuit present
in the computer, the chip will perform the AND operation.
Related Video: https://youtu.be/xBNdZ2ytTiU
Bitwise operators are applicable only
for int and Boolean types.
By mistake, if we are trying to apply for any other type then we will get
error.
print(4&5)-àValid
print(2.5&6.2)-àError:
print(2.3&2) -àError
>>> print(True&True)
True
>>> print(True&False)
False
>>> print(2&6)
2
2
|
2
|
|
2
|
1
|
0
|
2
|
0
|
1
|
Read the remainders from bottom to up
10 remaining 6 bits filled with 0s
i.e 000000 10
Read the remainders from bottom to up
Binary form of 6 is 110 (remaining 5 bits are filled with 0s) i.e
00000111
A&b is:
A:-à 0 0 0 0 0 0 1 0
B:à 0 0 0 0 0 1 1 1
-----------------
A&b= 0 0 0 0 0 0 1 0
------------------------
Now we need to convert from binary to decimal:
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
27
|
26
|
25
|
24
|
23
|
22
|
21
|
20
|
128
|
64
|
32
|
16
|
8
|
4
|
2
|
1
|
0
|
0
|
0
|
0
|
0
|
0
|
2
|
0
|
Add 1’s: 0+0+0+0+0+0+2+0=2
So result is 2
1.
Write down the
decimal number ex: 6
2.
Divide the decimal number by 2
3.
Write the result
underneath (below)
4.
5.
Divide the result
of the division by 2 and again write down the remainder
6. Continue dividing and writing down remainders until
the result of the division is 0
7.
Read the remainders
from bottom to up
Steps to convert binary to
decimal:
Converting from binary to
decimal involves multiplying the value of each digit.
1.
Write down the
number
2. Multiply the digit by value place holder
3.
Continue doing this
until you the number
Add
the result together (only 1’s)
Next Topic: Bitwise AND Operator (&)
Previous Topic: Membership Operators
Great blog !! thanks for sharing.
ReplyDeletePython Flask Training
Flask Framework
Python Flask Online Training
Informative blog. Thanks for sharing.
ReplyDeletePython Online Training
Python Online Training in Hyderabad