& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< left shift
>> right shift
~ one's complement (unary)
1.left shift <<;
a<<n ; n number of bits will shifted toward left.
Ex; 8<<2
binary representation of 8 ; 00001000 << 2
00001000<<2 ;
-------------------
00100000 ; answer is 32
2.Right shift <<;
a>>n ; n number of bits will shifted toward right.
Ex; 8>>2
binary representation of 8 ; 00001000 >>2
00001000>>2 ;
-------------------
00000010; answer is 2
Another way for calculating left and right shift: a<<n = a*2^n; a >> n = a/2^n ;
3.Bitewise AND &;
It is just like logical AND(&&) but we apply AND operation at binary level.
Example: 2 & 3 ;
2; 00000010
3; 00000011 &
---------------------
00000010 answer is 2
4.Bitewise OR |;
It is just like logical OR(||) but we apply OR operation at binary level.
Example: 2 | 3 ;
2; 00000010
3; 00000011 |
---------------------
00000011 answer is 3
5.Exclusive OR ^;
The ^ (caret) operator performs a bitwise exclusive-OR on two integers. Each bit in the result is 1 if one is 1 but not both, result will be 0 if both are 0 or 1.
Example: 2 ^ 3 ;
2; 00000010
3; 00000011 ^
---------------------
00000001 answer is 1
6.One's complement ~;
The ~ (tilde) operator performs a bitwise complement on its single integer operand. (The ~ operator is therefore a unary operator, like ! and the unary -, &, and * operators.). at binary level 0 will be converted into 1 and 1 will converted into 0.
~ 2 ; 00000010 ~
---------------------------
11111101 answer is -3
No comments:
Post a Comment