Re: binary representation of an integer

2008-06-24 Thread Terry Reedy
[EMAIL PROTECTED] wrote: On Jun 24, 10:38 am, Mark Dickinson <[EMAIL PROTECTED]> wrote: Interestingly, unlike hex and oct, bin doesn't add a trailing 'L' for longs: bin(13L) '0b1101' I wonder whether this is a bug... Strange in 2.6, but I know at least in 3.0 that all integers are C

Re: binary representation of an integer

2008-06-24 Thread bearophileHUGS
eliben: > Python's pack/unpack don't have the binary format for some reason, so > custom solutions have to be developed. One suggested in the ASPN > cookbook is:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/111286 > However, it is very general and thus inefficient. Try mine, it may be fa

Re: binary representation of an integer

2008-06-24 Thread cokofreedom
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)]

Re: binary representation of an integer

2008-06-24 Thread cokofreedom
On Jun 24, 10:38 am, Mark Dickinson <[EMAIL PROTECTED]> wrote: > On Jun 24, 9:03 am, eliben <[EMAIL PROTECTED]> wrote: > > > What would be the quickest way to do this ? I think that for dec2bin > > conversion, using hex() and then looping with a hex->bin lookup table > > would be probably much fast

Re: binary representation of an integer

2008-06-24 Thread Nick Craig-Wood
eliben <[EMAIL PROTECTED]> wrote: > I'm interested in converting integers to a binary representation, > string. I.e. a desired function would produce: > > dec2bin(13) => "1101" > > The other way is easily done in Python with the int() function. > > Perl has a very efficient way to do dec2bi

Re: binary representation of an integer

2008-06-24 Thread Mark Dickinson
On Jun 24, 9:03 am, eliben <[EMAIL PROTECTED]> wrote: > What would be the quickest way to do this ? I think that for dec2bin > conversion, using hex() and then looping with a hex->bin lookup table > would be probably much faster than the general baseconvert from the > recipe. I suspect you're righ

Re: binary representation of an integer

2008-06-24 Thread Maric Michaud
Le Tuesday 24 June 2008 10:03:58 eliben, vous avez écrit : > Hello, > > I'm interested in converting integers to a binary representation, > string. I.e. a desired function would produce: > > dec2bin(13) => "1101" > > The other way is easily done in Python with the int() function. > > Perl has a ver