Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16769

Re: Hints for writing bit-twiddling code in Python

References <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com>
Date 2011-12-06 22:08 -0800
Subject Re: Hints for writing bit-twiddling code in Python
From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3371.1323238116.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 12/6/11, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Hints for writing bit-twiddling code in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-07 04:03 +0000
  Re: Hints for writing bit-twiddling code in Python Dan Stromberg <drsalists@gmail.com> - 2011-12-06 22:08 -0800
  Re: Hints for writing bit-twiddling code in Python Peter Otten <__peter__@web.de> - 2011-12-07 09:21 +0100
  Re: Hints for writing bit-twiddling code in Python Serhiy Storchaka <storchaka@gmail.com> - 2011-12-07 11:33 +0200

csiph-web