binary representation of an integer

cokofreedom at gmail.com cokofreedom at gmail.com
Tue Jun 24 08:01:48 EDT 2008


And:

# return as a string
def itob_string(integer, count = 8):
    return "".join(str((integer >> i) & 1) for i in range(count - 1,
-1, -1))

# return as an iterator (i.e [0, 0, 0, 0, 1, 0, 1, 0])
def itob_list(integer, count = 8):
    return [(integer >> i) & 1 for i in range(count - 1, -1, -1)]

# return as a generator
def itob_generator(integer, count = 8):
    return ((integer >> i) & 1 for i in range(count - 1, -1, -1))



More information about the Python-list mailing list