Bitwise operators
BITWISE OPERATOR:
BITWISE Operator in python are used to perform bitwise calculations on numbers
(integers)
Ø
The name itself suggests that something to do
with binary format like bits.
Ø
We cannot directly perform operations on
integers so we need to convert them into binary format and then we need to do.
Ø
Result will be in decimal format.
Types of bitwise operators:
Ø
Bitwise And - &
Ø
Bitwise OR - |
Ø
Bitwise NOT - ~
Ø
Bitwise xOR - ^
Ø
Bitwise right shift - >>
Ø
Bitwise left shift - <<
1. Bitwise AND (&): this operator returns 1 if both the values are 1 or else it returns 0
x y x&y
1 1 1
1 0 0
0 1 0
0 0 0
Example: a=15 -
1111
B=6 -
0110
a & B = 0110
= 6 (DECIMAL)
2. Bitwise OR (|): If any one value is 1 then it returns 1 or else it returns zero.
x y
x|y
1 1 1
1 0 1
0 1 1
0 0
0
Example: a = 15 –
1111
B= 6 – 0110
a
| b = 1111
= 15 (decimal)
3. Bitwise NOT (~): ~(tilde) – to find complement of a given number we need to convert the number into binary format and after doing that you need to reverse the binary format of the number, which means u need to put 1 in place of zero and 0 in place of 1.
Example: a = 15 –
00001111(binary format of 15)
~a = ~15 – 11110000
*** remember one thing If you want compliment of a
particular number, then there is a trick that is compliment of any number will
be its next number with a negative sign.
Example: ~12 = -13
~ 24 = -25
·
if you want to find a compliment of negative
number then we need to find out 1 ~ compliment and 2 ~ COMPLIMENT
2~ = 1~ + 1
1 ~ IS NOTHING BUT REVERSING THE GIVEN
NUMBER (ONES COMPLIMENT IS NOTHING BUT BITWISE NOT) .
EXAMPLE: - 13 = 00001101
1~ = 11110010
2~ = 1~ + 1 (add 1 to the last element of one’s
compliment and that will be your 2~)
= 11110010
+ 1
-13 = 11110011
YOU CAN DOWNLOAD BITWISE OPERATORS PDF
4 . Bitwise XOR (^) : if one value is one and the other is zero (or) if both values are different only (or ) if there is only odd values of 1 then it returns one or else zero .
x
y x^y
1 1 0
1 0
1
0 1
1
0 0 0
Example:-
>>> 5^2
7
5=101(binary)
2=10(binary)
=111
Output=7
5 . Bitwise left shift (<<) : it is that we shift bits to the left side of the point .
Exp : 15 << 2
60
Explanation:
take a number 15 = 1111, we can take 1111 as 1111.0000 because 5.0 =5 and
5.0000 is also equal to 5 and both are same because no value will be there for
zeroes after the point right ! and we can take point (.) after value and zeroes
there will not be any change in the value
Take
1111.0000 and if we want to change bits to left by 2 then it will become
111100.00 because we
shifted two values to the left so if we want to do a left shift of 15 by 5 then
the output will be 111100000.00 like this, we will perform bitwise shift let
And value of left
shift of 15 by 2 bits is 111100.00 (60 in decimal format).
6 . Bitwise shift right (>>) : this is exactly vice versa of bitwise shift left ,in bitwise shift left we shift bits to the left of the point , in bitwise right we shift values to the right of the point .
Example: 15
>> 2
3
Explanation: take 15 (1111.00) and he asked a right shift
by 2 bits so the answer will be 11.1100 as we shifted 2 bits to right if we want
the right shift of 15 by 1 bit then output will be 111.100
1111.0000
=
11.1100
= 11(3 in decimal format)
Comments
Post a Comment