Matt23

int2bin & bin2int

Nov 14th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.25 KB | None | 0 0
  1. def int2bin(n):
  2.     res = ""
  3.     while n >= 1:
  4.         res += str(n % 2)
  5.         n /= 2
  6.     print res[::-1]
  7.  
  8. def bin2int(n):
  9.     res, exponent = 0, 0
  10.     while n >= 1:
  11.         res += (n % 10) * 2**exponent
  12.         n /= 10
  13.         exponent += 1
  14.     print res
  15.        
  16. int2bin(312)
  17. bin2int(100111000)
Advertisement
Add Comment
Please, Sign In to add comment