Bitwise Operators : Bitwise Operators do not operate on decimal value of the Number. They first convert the numbers into binary value & then they operate on it. Bitwise opeartors are not used with floating type variables.
- Bitwise AND :- &
- Bitwise OR :- |
- Bitwise XOR :- ^
- Bitwise NOT :- ~
- LEFT SHIFT :- <<
- RIGHT SHIFT :- >>
- Bitwise AND &
- Example :
- N1 = 10 , N2 = 15
- N1 & N2
- In Binary :
- 0 0 1 0 1 0
- 0 0 1 1 1 1
- ------------------
- 0 0 1 0 1 0
- So N1 & N2 == 15 (in decimal)
- Bitwise OR |
- Example :
- N1 = 10 , N2 = 15
- N1 | N2
- In Binary :
- 0 0 1 0 1 0
- 0 0 1 1 1 1
- ------------------
- 0 0 1 1 1 1
- So N1 | N2 == 10 (in decimal)
- Bitwise XOR ^
- Example :
- N1 = 10 , N2 = 15
- N1 ^ N2
- In Binary :
- 0 0 1 0 1 0
- 0 0 1 1 1 1
- ------------------
- 0 0 0 1 0 1
- So N1 ^ N2 == 5 (in decimal)
- Bitwise NOT - Numbers are
- Example :
- N1 = 10
- ~N1
- In Binary :
- 1 ... 0 0 1 0 1 0
- ------------------
- 1 ... 1 1 0 1 0 1
- 1 ... 0 0 1 0 1 0 (Take Complement Leaving Sign Bit)
- 1 ... 0 0 1 0 1 1 (Add 1)
- So ~N1 == -11 (in decimal)
- Another Example :
- N2 = 15
- ~N2
- In Binary :
- 1 ... 0 0 1 1 1 1
- ------------------
- 1 ... 1 1 0 0 0 0
- 1 ... 0 0 1 1 1 1 (Take Complement Leaving Sign Bit)
- 1 ... 0 1 0 0 0 0 (Add 1)
- So ~N1 == -16 (in decimal)
- Bitwise LEFT SHIFT <<
- Example :
- N1 = 10
- N1<< 2
- In Binary :
- 0 0 1 0 1 0 (Left Shift shifts the bits to the left & fills 0)
- -----------------
- 1 0 1 0 0 0
- So N1 << 2 == 40 (in decimal)
- Bitwise RIGHT SHIFT >>
- Example :
- N1 = 10
- N1>> 2
- In Binary :
- 0 0 1 0 1 0 (Left Shift shifts the bits to the left & fills 0)
- -----------------
- 0 0 0 0 1 0
- So N1 >> 2 == 2 (in decimal)
Comments
Post a Comment