Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_bitwise_operators.py
- a = 9
- b = 65
- def io(n):
- return "{0:08b}".format(n)
- def a2b():
- print io(a)+' on '+io(b)+' = '+io(t)
- def nnn(n):
- print io(n)+' to '+io(t)
- t = a & b
- print "Bitwise (& ) AND Operator on",a,"and",b,"is =", t
- a2b()
- t = a | b
- print "Bitwise (| ) OR Operator on",a,"and",b,"is =", t
- a2b()
- t = a ^ b
- print "Bitwise (^ ) EXCLUSIVE OR Operator on",a,"and",b,"is =", t
- a2b()
- t = ~a
- print "Bitwise (~ ) NOT Operator on",a,"is =", t
- nnn(a)
- t = ~b
- print "Bitwise (~ ) NOT Operator on",b,"is =", t
- nnn(b)
- t = a << 1
- print "Bitwise (<<) LEFT SHIFT Operator on",a,"is =", t
- nnn(a)
- t = b << 1
- print "Bitwise (<<) LEFT SHIFT Operator on",b,"is =", t
- nnn(b)
- t = a >> 1
- print "Bitwise (>>) RIGHT SHIFT Operator on",a,"is =", t
- nnn(a)
- t = b >> 1
- print "Bitwise (>>) RIGHT SHIFT Operator on",b,"is =", t
- nnn(b)
- print
- def binaryToDecimal(n):
- print int(n,2)
- print io(9876543210123)
- binaryToDecimal('100')
- binaryToDecimal('11111111')
- binaryToDecimal('10001111101110001111110110011000001010001011')
- '''
- Bitwise (& ) AND Operator on 9 and 65 is = 1
- 00001001 on 01000001 = 00000001
- Bitwise (| ) OR Operator on 9 and 65 is = 73
- 00001001 on 01000001 = 01001001
- Bitwise (^ ) EXCLUSIVE OR Operator on 9 and 65 is = 72
- 00001001 on 01000001 = 01001000
- Bitwise (~ ) NOT Operator on 9 is = -10
- 00001001 to -0001010
- Bitwise (~ ) NOT Operator on 65 is = -66
- 01000001 to -1000010
- Bitwise (<<) LEFT SHIFT Operator on 9 is = 18
- 00001001 to 00010010
- Bitwise (<<) LEFT SHIFT Operator on 65 is = 130
- 01000001 to 10000010
- Bitwise (>>) RIGHT SHIFT Operator on 9 is = 4
- 00001001 to 00000100
- Bitwise (>>) RIGHT SHIFT Operator on 65 is = 32
- 01000001 to 00100000
- 10001111101110001111110110011000001010001011
- 4
- 255
- 9876543210123
- '''
Add Comment
Please, Sign In to add comment