#Python cookbook, 18.11
#note:each hex is 4-bit binary equivalent
#one bit at a time
def integer_to_binary(n):
bits = [] #bits = [None]*8
while n:
#print bits
bits.append('01'[n&1])
n >>= 1
bits.reverse()
print bits
integer_to_binary(16)
'''
try:
if 1>0:
i = 1/0
print 'in try'
except Exception, e: #GO HERE
print 'exception caught', e
else: #OR GO HERE
print 'in else'
#Then continue from here after "except" or "else"
print 'last line'
'''
No comments:
Post a Comment