sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Bitwise Operators

Bitwise Operators

Bitwise OperatorSign
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 ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
40 0 0 0   0 1 0 0


Operation on OR(a|b)
Decimal ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
280 0 0 1   1 1 0 0

Operation on XOR(a^b)
Decimal ValueBinary Value
अगर a = 20, b = 12 हो तो,
200 0 0 1   0 1 0 0
120 0 0 0   1 1 0 0
240 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 ValueBinary Value
~120 0 0 0   1 1 0 0
2431 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 ValueBinary Value2's Complement
2431111 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 

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply