Converting integer to binary representation

M

Mark Dufour

Hi all,

I need to convert an integer into some binary representation. I found the
following code in the online cookbook (adapted to return a list):

binary = lambda n: n>0 and [n&1]+binary(n>>1) or []

This is sure nice, but I'm wondering why something like this doesn't seem to
be in the standard library, for example by formatting with '%b' % number. I
can't think of a any reason for not doing it this way, as working with binary
representations is quite common in programming, but I guess I'm not a
language designer.. (just yet ;)) Or is there another nice way of doing this
which I overlooked?


Thanks,
Mark Dufour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top