Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16774
| 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!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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.04; 'think,': 0.05; 'subject:Python': 0.05; 'bits': 0.07; 'subject:code': 0.07; 'python': 0.08; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'python?': 0.15; 'coerced': 0.16; 'ord': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:bit': 0.16; 'subject:writing': 0.16; 'this:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'written': 0.20; 'java': 0.21; 'trying': 0.21; 'from:addr:web.de': 0.23; 'translated': 0.23; 'byte': 0.24; "python's": 0.24; 'code': 0.25; "i'm": 0.26; 'problem': 0.29; 'seed': 0.30; '(the': 0.30; 'anyone': 0.31; "i've": 0.31; 'signed': 0.32; 'header:X-Complaints-To:1': 0.33; 'to:addr:python- list': 0.34; 'character': 0.34; 'integer': 0.34; 'think': 0.37; 'steven': 0.38; 'received:org': 0.38; 'some': 0.38; 'getting': 0.38; 'e.g.': 0.39; 'else': 0.39; 'help': 0.39; 'to:addr:python.org': 0.40; 'range': 0.61; 'results': 0.63; 'due': 0.66; 'account.': 0.80 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Hints for writing bit-twiddling code in Python |
| Followup-To | gmane.comp.python.general |
| Date | Wed, 07 Dec 2011 09:21:55 +0100 |
| Organization | None |
| References | <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b8d0.dip.t-dialin.net |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3373.1323246133.27778.python-list@python.org> (permalink) |
| Lines | 34 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323246133 news.xs4all.nl 6916 [2001:888:2000:d::a6]:52451 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16774 |
Followups directed to: gmane.comp.python.general
Show key headers only | View raw
Steven D'Aprano wrote:
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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