a problem to solve

mensanator at aol.com mensanator at aol.com
Sun Mar 26 22:23:07 EST 2006


Clemens Hepper wrote:
> Hi,
>
> mensanator at aol.com wrote:
> > That's one way to do it. I did it that way because I have the
> > hex patterns memorized.
>
> You should be able to generate your numbers like this:
>
> number = int('0010010001000000100', 2)

Well, that would be another way, wouldn't it?

Thanks for the tip, but you're a bit late (pun intended),
we've both already solved the problem. And since base
conversion is a one-way street in Python, the fact that
you can do that doesn't eliminate the need for gmpy
(or something equivalent) if you want to go the other
way, decimal to binary.

And furthermore, having Python's bitwise operators
is nice, but it's not nice enough. I need the bitwise
functionality gmpy provides that's not available in
Python: scan for position of least significant 1 or 0,
count of 1 bits, Hamming distance, etc.

So, rather than point out that one can do

 number = int('0010010001000000100', 2)

I would rather advise that the person obtain gmpy
and use its conversion

 number = gmpy.mpz('0010010001000000100', 2)

They'll thank me eventually.

> 
> mfg
> - eth




More information about the Python-list mailing list