fork download
  1. #!/usr/bin/python
  2. """
  3. Program to do binary representation of various interesting ints.
  4. """
  5.  
  6.  
  7. def convert_to_binary(n):
  8. binary = []
  9. while n:
  10. binary.append(str(n%2))
  11. n/=2
  12. binary.reverse()
  13. return "".join(binary)
  14.  
  15.  
  16. for i in range(20,30):
  17. print(i, convert_to_binary(i))
  18.  
  19. hexa_values = ['0','1','A','FF','DEADBEEF','CAFEBABE']
  20. for each in hexa_values:
  21. dec = int(each, 16)
  22. print(each, convert_to_binary(dec))
  23.  
  24. """
  25. Find out if the machine is storing it in the one's complement or two's
  26. complement.
  27. 1 is stored as 0000 0001
  28. -1 in 1's complement is 1111 1110
  29. -1 in 2's complement is 1111 1111
  30. """
  31.  
  32. import struct
  33.  
  34. if (ord(struct.pack('b',-1)[0]) == 255):
  35. print('twos complement')
  36. else:
  37. print('ones complement')
  38.  
  39. for i in range(200,255):
  40. print(hex(i))
  41.  
  42. for i in range(0,256):
  43. print(chr(i), i, hex(i))
  44.  
  45. """
  46. Binary Addition and Subtraction
  47. """
  48. a = 20
  49. b = 10
  50.  
  51. a_bin = convert_to_binary(a)
  52. b_bin = convert_to_binary(b)
  53.  
  54. if (len(a_bin) > len(b_bin)):
  55. b_bin = b_bin.rjust(len(a_bin),'0')
  56. elif (len(a_bin) < len(b_bin)):
  57. a_bin = a_bin.rjust(len(b_bin),'0')
  58.  
  59. def sum_bin(a, b):
  60. rules = {('0', '0'):(0,0),
  61. ('0', '1'):(1,0),
  62. ('1', '0'):(1,0),
  63. ('1', '1'):(0,1)
  64. }
  65. carry = 0
  66. sum = 0
  67. result = ""
  68. for x,y in zip(reversed(a),reversed(b)):
  69. sum = rules[(x,y)][0]
  70. if carry:
  71. sum = rules[(str(sum), str(carry))][0]
  72. result += str(sum)
  73. carry = rules[(x,y)][1]
  74.  
  75. if carry:
  76. result += str(1)
  77.  
  78. return result[::-1]
  79.  
  80. def sub_bin(a, b):
  81. ones_complement = ""
  82. for c in b:
  83. if c == '0':
  84. ones_complement += '1'
  85. elif c == '1':
  86. ones_complement += '0'
  87.  
  88. b = ones_complement
  89. b = sum_bin(b,'1'.rjust(len(b),'0'))
  90.  
  91. rules = {('0', '0'):(0,0),
  92. ('0', '1'):(1,0),
  93. ('1', '0'):(1,0),
  94. ('1', '1'):(0,1)
  95. }
  96.  
  97. carry = 0
  98. sum = 0
  99. result = ""
  100. for x,y in zip(reversed(a),reversed(b)):
  101. sum = rules[(x,y)][0]
  102. if carry:
  103. sum = rules[(str(sum), str(carry))][0]
  104. result += str(sum)
  105. carry = rules[(x,y)][1]
  106.  
  107. # unlike addition carry should be discarded.
  108.  
  109. return result[::-1]
  110.  
  111.  
  112. print('a', a, a_bin)
  113. print('b', b, b_bin)
  114.  
  115. print('a+b ',sum_bin(a_bin, b_bin))
  116. print('a-b ',sub_bin(a_bin, b_bin))
  117.  
  118. # your code goes here
Success #stdin #stdout 0.02s 6992KB
stdin
Standard input is empty
stdout
(20, '10100')
(21, '10101')
(22, '10110')
(23, '10111')
(24, '11000')
(25, '11001')
(26, '11010')
(27, '11011')
(28, '11100')
(29, '11101')
('0', '')
('1', '1')
('A', '1010')
('FF', '11111111')
('DEADBEEF', '11011110101011011011111011101111')
('CAFEBABE', '11001010111111101011101010111110')
twos complement
0xc8
0xc9
0xca
0xcb
0xcc
0xcd
0xce
0xcf
0xd0
0xd1
0xd2
0xd3
0xd4
0xd5
0xd6
0xd7
0xd8
0xd9
0xda
0xdb
0xdc
0xdd
0xde
0xdf
0xe0
0xe1
0xe2
0xe3
0xe4
0xe5
0xe6
0xe7
0xe8
0xe9
0xea
0xeb
0xec
0xed
0xee
0xef
0xf0
0xf1
0xf2
0xf3
0xf4
0xf5
0xf6
0xf7
0xf8
0xf9
0xfa
0xfb
0xfc
0xfd
0xfe
('\x00', 0, '0x0')
('\x01', 1, '0x1')
('\x02', 2, '0x2')
('\x03', 3, '0x3')
('\x04', 4, '0x4')
('\x05', 5, '0x5')
('\x06', 6, '0x6')
('\x07', 7, '0x7')
('\x08', 8, '0x8')
('\t', 9, '0x9')
('\n', 10, '0xa')
('\x0b', 11, '0xb')
('\x0c', 12, '0xc')
('\r', 13, '0xd')
('\x0e', 14, '0xe')
('\x0f', 15, '0xf')
('\x10', 16, '0x10')
('\x11', 17, '0x11')
('\x12', 18, '0x12')
('\x13', 19, '0x13')
('\x14', 20, '0x14')
('\x15', 21, '0x15')
('\x16', 22, '0x16')
('\x17', 23, '0x17')
('\x18', 24, '0x18')
('\x19', 25, '0x19')
('\x1a', 26, '0x1a')
('\x1b', 27, '0x1b')
('\x1c', 28, '0x1c')
('\x1d', 29, '0x1d')
('\x1e', 30, '0x1e')
('\x1f', 31, '0x1f')
(' ', 32, '0x20')
('!', 33, '0x21')
('"', 34, '0x22')
('#', 35, '0x23')
('$', 36, '0x24')
('%', 37, '0x25')
('&', 38, '0x26')
("'", 39, '0x27')
('(', 40, '0x28')
(')', 41, '0x29')
('*', 42, '0x2a')
('+', 43, '0x2b')
(',', 44, '0x2c')
('-', 45, '0x2d')
('.', 46, '0x2e')
('/', 47, '0x2f')
('0', 48, '0x30')
('1', 49, '0x31')
('2', 50, '0x32')
('3', 51, '0x33')
('4', 52, '0x34')
('5', 53, '0x35')
('6', 54, '0x36')
('7', 55, '0x37')
('8', 56, '0x38')
('9', 57, '0x39')
(':', 58, '0x3a')
(';', 59, '0x3b')
('<', 60, '0x3c')
('=', 61, '0x3d')
('>', 62, '0x3e')
('?', 63, '0x3f')
('@', 64, '0x40')
('A', 65, '0x41')
('B', 66, '0x42')
('C', 67, '0x43')
('D', 68, '0x44')
('E', 69, '0x45')
('F', 70, '0x46')
('G', 71, '0x47')
('H', 72, '0x48')
('I', 73, '0x49')
('J', 74, '0x4a')
('K', 75, '0x4b')
('L', 76, '0x4c')
('M', 77, '0x4d')
('N', 78, '0x4e')
('O', 79, '0x4f')
('P', 80, '0x50')
('Q', 81, '0x51')
('R', 82, '0x52')
('S', 83, '0x53')
('T', 84, '0x54')
('U', 85, '0x55')
('V', 86, '0x56')
('W', 87, '0x57')
('X', 88, '0x58')
('Y', 89, '0x59')
('Z', 90, '0x5a')
('[', 91, '0x5b')
('\\', 92, '0x5c')
(']', 93, '0x5d')
('^', 94, '0x5e')
('_', 95, '0x5f')
('`', 96, '0x60')
('a', 97, '0x61')
('b', 98, '0x62')
('c', 99, '0x63')
('d', 100, '0x64')
('e', 101, '0x65')
('f', 102, '0x66')
('g', 103, '0x67')
('h', 104, '0x68')
('i', 105, '0x69')
('j', 106, '0x6a')
('k', 107, '0x6b')
('l', 108, '0x6c')
('m', 109, '0x6d')
('n', 110, '0x6e')
('o', 111, '0x6f')
('p', 112, '0x70')
('q', 113, '0x71')
('r', 114, '0x72')
('s', 115, '0x73')
('t', 116, '0x74')
('u', 117, '0x75')
('v', 118, '0x76')
('w', 119, '0x77')
('x', 120, '0x78')
('y', 121, '0x79')
('z', 122, '0x7a')
('{', 123, '0x7b')
('|', 124, '0x7c')
('}', 125, '0x7d')
('~', 126, '0x7e')
('\x7f', 127, '0x7f')
('\x80', 128, '0x80')
('\x81', 129, '0x81')
('\x82', 130, '0x82')
('\x83', 131, '0x83')
('\x84', 132, '0x84')
('\x85', 133, '0x85')
('\x86', 134, '0x86')
('\x87', 135, '0x87')
('\x88', 136, '0x88')
('\x89', 137, '0x89')
('\x8a', 138, '0x8a')
('\x8b', 139, '0x8b')
('\x8c', 140, '0x8c')
('\x8d', 141, '0x8d')
('\x8e', 142, '0x8e')
('\x8f', 143, '0x8f')
('\x90', 144, '0x90')
('\x91', 145, '0x91')
('\x92', 146, '0x92')
('\x93', 147, '0x93')
('\x94', 148, '0x94')
('\x95', 149, '0x95')
('\x96', 150, '0x96')
('\x97', 151, '0x97')
('\x98', 152, '0x98')
('\x99', 153, '0x99')
('\x9a', 154, '0x9a')
('\x9b', 155, '0x9b')
('\x9c', 156, '0x9c')
('\x9d', 157, '0x9d')
('\x9e', 158, '0x9e')
('\x9f', 159, '0x9f')
('\xa0', 160, '0xa0')
('\xa1', 161, '0xa1')
('\xa2', 162, '0xa2')
('\xa3', 163, '0xa3')
('\xa4', 164, '0xa4')
('\xa5', 165, '0xa5')
('\xa6', 166, '0xa6')
('\xa7', 167, '0xa7')
('\xa8', 168, '0xa8')
('\xa9', 169, '0xa9')
('\xaa', 170, '0xaa')
('\xab', 171, '0xab')
('\xac', 172, '0xac')
('\xad', 173, '0xad')
('\xae', 174, '0xae')
('\xaf', 175, '0xaf')
('\xb0', 176, '0xb0')
('\xb1', 177, '0xb1')
('\xb2', 178, '0xb2')
('\xb3', 179, '0xb3')
('\xb4', 180, '0xb4')
('\xb5', 181, '0xb5')
('\xb6', 182, '0xb6')
('\xb7', 183, '0xb7')
('\xb8', 184, '0xb8')
('\xb9', 185, '0xb9')
('\xba', 186, '0xba')
('\xbb', 187, '0xbb')
('\xbc', 188, '0xbc')
('\xbd', 189, '0xbd')
('\xbe', 190, '0xbe')
('\xbf', 191, '0xbf')
('\xc0', 192, '0xc0')
('\xc1', 193, '0xc1')
('\xc2', 194, '0xc2')
('\xc3', 195, '0xc3')
('\xc4', 196, '0xc4')
('\xc5', 197, '0xc5')
('\xc6', 198, '0xc6')
('\xc7', 199, '0xc7')
('\xc8', 200, '0xc8')
('\xc9', 201, '0xc9')
('\xca', 202, '0xca')
('\xcb', 203, '0xcb')
('\xcc', 204, '0xcc')
('\xcd', 205, '0xcd')
('\xce', 206, '0xce')
('\xcf', 207, '0xcf')
('\xd0', 208, '0xd0')
('\xd1', 209, '0xd1')
('\xd2', 210, '0xd2')
('\xd3', 211, '0xd3')
('\xd4', 212, '0xd4')
('\xd5', 213, '0xd5')
('\xd6', 214, '0xd6')
('\xd7', 215, '0xd7')
('\xd8', 216, '0xd8')
('\xd9', 217, '0xd9')
('\xda', 218, '0xda')
('\xdb', 219, '0xdb')
('\xdc', 220, '0xdc')
('\xdd', 221, '0xdd')
('\xde', 222, '0xde')
('\xdf', 223, '0xdf')
('\xe0', 224, '0xe0')
('\xe1', 225, '0xe1')
('\xe2', 226, '0xe2')
('\xe3', 227, '0xe3')
('\xe4', 228, '0xe4')
('\xe5', 229, '0xe5')
('\xe6', 230, '0xe6')
('\xe7', 231, '0xe7')
('\xe8', 232, '0xe8')
('\xe9', 233, '0xe9')
('\xea', 234, '0xea')
('\xeb', 235, '0xeb')
('\xec', 236, '0xec')
('\xed', 237, '0xed')
('\xee', 238, '0xee')
('\xef', 239, '0xef')
('\xf0', 240, '0xf0')
('\xf1', 241, '0xf1')
('\xf2', 242, '0xf2')
('\xf3', 243, '0xf3')
('\xf4', 244, '0xf4')
('\xf5', 245, '0xf5')
('\xf6', 246, '0xf6')
('\xf7', 247, '0xf7')
('\xf8', 248, '0xf8')
('\xf9', 249, '0xf9')
('\xfa', 250, '0xfa')
('\xfb', 251, '0xfb')
('\xfc', 252, '0xfc')
('\xfd', 253, '0xfd')
('\xfe', 254, '0xfe')
('\xff', 255, '0xff')
('a', 20, '10100')
('b', 10, '01010')
('a+b ', '11110')
('a-b ', '01010')