Hints for writing bit-twiddling code in Python

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I have some bit-twiddling code written in Java which I am trying to port
to Python. I'm not getting the same results though, and I think the
problem is due to differences between Java's signed byte/int/long types,
and Python's unified long integer type. E.g. Java's >>> is not exactly
the same as Python's >> operator, and a character coerced to a byte in
Java is not the same as ord(char) in Python. (The Java byte is in the
range -128...127, I think, while the ord in Python is in 0...255.)

Can anyone point me to some good resources to help me port the Java code
to Python?

If it helps, the Java code includes bits like this:

long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L;
while (newSeed >= 0x80000000L) {
newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L);
}
seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed;


which I've translated into:

newseed = (seed & 0xFFFFFFFF)*0x41A7
while (newseed >= 0x80000000):
newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31)
seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF
 
D

Dan Stromberg

I have some bit-twiddling code written in Java which I am trying to port
to Python.:

long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L;
while (newSeed >= 0x80000000L) {
newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L);
}
seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed;

I suspect the problem lies somewhere other than the java and python
code you posted.

I'm having a bit of a time finding an input value of seed that gives
two different results, despite throwing some hard-feeling test cases
and lots of random values, using both a 32 bit and a 64 bit JVM.

I believe java likes to treat strings like Python 3, but if you use
"export LC_ALL=en_US.ISO-8859-1", then it'll behave a little more like
Python 2 in that strings have an 8 bit encoding (or at least act like
it) that's round-tripable.

If worse comes to worse, you could probably write some test harness
code for each of the java and python, and then feed them the same
inputs - to see which pieces differ and which don't.
 
P

Peter Otten

Steven said:
I have some bit-twiddling code written in Java which I am trying to port
to Python. I'm not getting the same results though, and I think the
problem is due to differences between Java's signed byte/int/long types,
and Python's unified long integer type. E.g. Java's >>> is not exactly
the same as Python's >> operator, and a character coerced to a byte in
Java is not the same as ord(char) in Python. (The Java byte is in the
range -128...127, I think, while the ord in Python is in 0...255.)

Can anyone point me to some good resources to help me port the Java code
to Python?

If it helps, the Java code includes bits like this:

long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L;
while (newSeed >= 0x80000000L) {
newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L);
}
seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed;


which I've translated into:

newseed = (seed & 0xFFFFFFFF)*0x41A7
while (newseed >= 0x80000000):
newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31)
seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF

I think you need to take negative ints into account. Try adding

if seed & 0x80000000: # 2**31
seed -= 0x100000000 # 2**32, two's complement
 
S

Serhiy Storchaka

07.12.11 06:03, Steven D'Aprano напиÑав(ла):
long newSeed = (seed& 0xFFFFFFFFL) * 0x41A7L;
while (newSeed>= 0x80000000L) {
newSeed = (newSeed& 0x7FFFFFFFL) + (newSeed>>> 31L);
}
seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed;

seed = (seed & 0xFFFFFFFF) * 0x41A7 % 0x7FFFFFFF
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top