here2share

# b_bitwise_operators.py

Mar 27th, 2021 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # b_bitwise_operators.py
  2.  
  3. a = 9
  4. b = 65
  5.  
  6. def io(n):
  7.     return "{0:08b}".format(n)
  8.  
  9. def a2b():
  10.     print io(a)+' on '+io(b)+' = '+io(t)
  11.  
  12. def nnn(n):
  13.     print io(n)+' to '+io(t)
  14.  
  15. t = a & b
  16. print "Bitwise (& ) AND Operator on",a,"and",b,"is =", t
  17. a2b()
  18. t = a | b
  19. print "Bitwise (| ) OR Operator on",a,"and",b,"is =", t
  20. a2b()
  21. t = a ^ b
  22. print "Bitwise (^ ) EXCLUSIVE OR Operator on",a,"and",b,"is =", t
  23. a2b()
  24. t = ~a
  25. print "Bitwise (~ ) NOT Operator on",a,"is =", t
  26. nnn(a)
  27. t = ~b
  28. print "Bitwise (~ ) NOT Operator on",b,"is =", t
  29. nnn(b)
  30. t = a << 1
  31. print "Bitwise (<<) LEFT SHIFT Operator on",a,"is =", t
  32. nnn(a)
  33. t = b << 1
  34. print "Bitwise (<<) LEFT SHIFT Operator on",b,"is =", t
  35. nnn(b)
  36. t = a >> 1
  37. print "Bitwise (>>) RIGHT SHIFT Operator on",a,"is =", t
  38. nnn(a)
  39. t = b >> 1
  40. print "Bitwise (>>) RIGHT SHIFT Operator on",b,"is =", t
  41. nnn(b)
  42. print
  43.  
  44. def binaryToDecimal(n):
  45.     print int(n,2)
  46.  
  47. print io(9876543210123)
  48. binaryToDecimal('100')
  49. binaryToDecimal('11111111')
  50. binaryToDecimal('10001111101110001111110110011000001010001011')
  51.  
  52. '''
  53. Bitwise (& ) AND Operator on 9 and 65 is = 1
  54. 00001001 on 01000001 = 00000001
  55. Bitwise (| ) OR Operator on 9 and 65 is = 73
  56. 00001001 on 01000001 = 01001001
  57. Bitwise (^ ) EXCLUSIVE OR Operator on 9 and 65 is = 72
  58. 00001001 on 01000001 = 01001000
  59. Bitwise (~ ) NOT Operator on 9 is = -10
  60. 00001001 to -0001010
  61. Bitwise (~ ) NOT Operator on 65 is = -66
  62. 01000001 to -1000010
  63. Bitwise (<<) LEFT SHIFT Operator on 9 is = 18
  64. 00001001 to 00010010
  65. Bitwise (<<) LEFT SHIFT Operator on 65 is = 130
  66. 01000001 to 10000010
  67. Bitwise (>>) RIGHT SHIFT Operator on 9 is = 4
  68. 00001001 to 00000100
  69. Bitwise (>>) RIGHT SHIFT Operator on 65 is = 32
  70. 01000001 to 00100000
  71.  
  72. 10001111101110001111110110011000001010001011
  73. 4
  74. 255
  75. 9876543210123
  76. '''
  77.  
Add Comment
Please, Sign In to add comment