Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'subject:Python': 0.05; 'behave': 0.07; 'subject:code': 0.07; 'worse': 0.07; 'python': 0.08; 'python.:': 0.16; 'subject:bit': 0.16; 'subject:writing': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'worse,': 0.16; 'cc:addr:python-list': 0.16; 'received:74.125.82.44': 0.16; 'received:mail-ww0-f44.google.com': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'cc:no real name:2**0': 0.20; 'written': 0.20; 'java': 0.21; 'trying': 0.21; 'input': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'somewhere': 0.23; 'differ': 0.23; 'pieces': 0.23; 'cc:2**0': 0.24; 'suspect': 0.24; 'code': 0.25; "i'm": 0.26; 'random': 0.28; 'bit': 0.28; 'message- id:@mail.gmail.com': 0.28; 'likes': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'seed': 0.30; 'throwing': 0.30; 'least': 0.30; 'cases': 0.32; 'probably': 0.34; 'received:74.125.82': 0.35; 'test': 0.35; 'encoding': 0.37; 'two': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'using': 0.38; 'despite': 0.38; 'steven': 0.38; 'some': 0.38; 'feed': 0.39; 'finding': 0.39; 'more': 0.61; 'believe': 0.65; 'act': 0.65; 'it)': 0.67; 'harness': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=MaVDJySmopjB7ME36gxMh6LGaA/ZkbWa0rafvLItH1A=; b=V3z3+6G+a/8FukF0TcJWgfI9AOl5nQficn3W3JHQYdauDgrHNIvlKP42+1YffyMpqh nDuBsUOgL7FDx1dThlADNvCdy97C9h0D+LyE+Ve4LbBiZYgvsaMwlDycS1eLgUxouvmu ihCOcz3JCMyhDFPGT1a8CEosURjaDO31ccxA0= MIME-Version: 1.0 In-Reply-To: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Date: Tue, 6 Dec 2011 22:08:34 -0800 Subject: Re: Hints for writing bit-twiddling code in Python From: Dan Stromberg To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323238116 news.xs4all.nl 6888 [2001:888:2000:d::a6]:45350 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16769 On 12/6/11, Steven D'Aprano 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.