binary representaion of a number
Neil Schemenauer
nascheme at enme.ucalgary.ca
Mon Aug 21 00:11:07 EDT 2000
David Broadwell <dbroadwell at mindspring.com> wrote:
>here is what i am looking for:
>>>> bin(i)
>'111100000011110001000000000001'
See the attached code for a reasonably efficient Python version
that could easily be extended to handle long integers.
Neil
--
"Everyone can be taught to sculpt: Michelangelo would have had to
be taught how not to. So it is with the great programmers" -- Alan Perlis
import string
hexmap = {
"0": "0000",
"1": "0001",
"2": "0010",
"3": "0011",
"4": "0100",
"5": "0101",
"6": "0110",
"7": "0111",
"8": "1000",
"9": "1001",
"a": "1010",
"b": "1011",
"c": "1100",
"d": "1101",
"e": "1110",
"f": "1111",
}
def bin(n):
bits = []
for digit in ("%0.8x" % n):
bits.append(hexmap[digit])
return string.join(bits, '')
More information about the Python-list
mailing list