Bitwise Operators
| Bitwise Operator | Sign |
| Bitwise AND | & |
| Bitwise OR | | |
| Bitwise XOR | ^ |
| Bitwise Complement | ~ |
| Bitwise Left Shift | << |
| Bitwise Right Shift | >> |
Truth Table for &, |, ^
| a | b |
a & b | a | b |
a ^ b |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
| Operation on AND(a&b) |
| Decimal Value | Binary Value |
| अगर a = 20, b = 12 हो तो, |
| 20 | 0 0 0 1 0 1 0 0 |
| 12 | 0 0 0 0 1 1 0 0 |
| 4 | 0 0 0 0 0 1 0 0 |
| Operation on OR(a|b) |
| Decimal Value | Binary Value |
| अगर a = 20, b = 12 हो तो, |
| 20 | 0 0 0 1 0 1 0 0 |
| 12 | 0 0 0 0 1 1 0 0 |
| 28 | 0 0 0 1 1 1 0 0 |
| Operation on XOR(a^b) |
| Decimal Value | Binary Value |
| अगर a = 20, b = 12 हो तो, |
| 20 | 0 0 0 1 0 1 0 0 |
| 12 | 0 0 0 0 1 1 0 0 |
| 24 | 0 0 0 1 1 0 0 0 |
Binary Left Shift( << ) and Right Shift( >> )
- Left Shift(<<) for e.g. a=20; /* 0001 0100 */ a << 2 में
numeric value के binary value में हर binary number को 2 binary numbers
left side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो इसका 0101
0000 मतलब 80 हो जायेगा |
- Right Shift(>>) for e.g. a=20; /* 0001 0100 */ ये Left shift
से बिलकुल उलट है | Right Shift a>> 2 में numeric value के binary
value में हर binary number को 2 binary numbers right side से shift करता
है | for e.g.a=20; /* 0001 0100 */ तो इसका 0000 0101 मतलब 5 हो जायेगा |
Complement Operator (~)
- ये Operator सारे bit reverse करता है |
- ये Operator 0 को 1 कर देता है और 1 को 0 कर देता है |
| Operation on Complement( ~ ) |
| Decimal Value | Binary Value |
| ~12 | 0 0 0 0 1 1 0 0 |
| 243 | 1 1 1 1 0 0 1 1 |
यहाँ पर Output -13 आने के बजाय 243 आया ऐसा क्यों ?
2's Complement of 243 -(reverse of 243 in binary + 1)
| Operation on 2's Complement( ~ ) |
| Decimal Value | Binary Value | 2's Complement |
| 243 | 1111 0011 | -(0000 1100+1) = -(0000 1101) = -13(output) |
For eg.
Source Code :
a = 20 # 0001 0100
b = 12 # 0000 1100
c = a & b
print("value of c is ",c) # 4 = 0000 0100
c = a | b
print("value of c is ",c) # 28 = 0001 1100
c = a ^ b
print("value of c is ",c) # 24 = 0001 1000
c = a << 2
print("value of c is ",c) # 80 = 0101 0000
c=a >> 2
print("value of c is ",c) # 5 = 0000 0101
print("value of b is ",~b) # -13 = 1111 0011
Output
value of c is 4
value of c is 28
value of c is 24
value of c is 80
value of b is 5
value of c is -13
No comments: