Number of bits/sizeof int

Jon Clements joncle at googlemail.com
Sat Jan 31 08:44:04 CET 2009


On Jan 31, 7:29 am, John Machin <sjmac... at lexicon.net> wrote:
> On Jan 31, 6:03 pm, Jon Clements <jon... at googlemail.com> wrote:
>
> > Hi Group,
>
> > This has a certain amount of irony (as this is what I'm pretty much
> > after):-
> > Fromhttp://docs.python.org/dev/3.0/whatsnew/3.1.html:
> > "The int() type gained a bit_length method that returns the number of
> > bits necessary to represent its argument in binary:"
>
> > Any tips on how to get this in 2.5.2 as that's the production version
> > I'm stuck with.
>
> For a positive integer, the simplest method (not necessarily the
> fastest) is to count how many times you need to shift it right to make
> it become zero.
> For non-positive integers, what answers do you want?

Thankfully, all integers are positive!

Must have been having a blonde moment, 'cos as soon as I read your
answer it was "of course that works". Not concerned about 'fast' as I
hardly think with DB and disk I/O I'm going to worry about bit-
shifting...

Much appreciated John,

Jon.



More information about the Python-list mailing list