Friday, September 12, 2008

Binary representation in Python

Give a one line expression to convert a number to its binary
representation in Python. For example, 258 to "100000010".

1 comment:

Kundan Singh said...

If number is in data, then the following expression converts.

''.join([''.join([('1' if (ord(x) & (1<<(7-y))) else '0') for y in range(0, 8)]) for x in data])