Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
Well, that about wraps this up...MRAB was 100% correct, that solution worked...not sure how I managed to mess it up when I tried it early. Based on the incoming values of u here is the code with the minimal number of maskings: def findit(u): mask = 0x u += 0xe91aaa35 u ^= u >>

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
On Jul 10, 4:04 pm, Harald Luessen <[EMAIL PROTECTED]> wrote: > On Thu, 10 Jul 2008 Jordan wrote: > > > > >On Jul 10, 1:35 pm, MRAB <[EMAIL PROTECTED]> wrote: > >> On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote: > > >> > I am trying to rewrite some C source code for a poker hand evaluator > >

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Harald Luessen
On Thu, 10 Jul 2008 Jordan wrote: >On Jul 10, 1:35 pm, MRAB <[EMAIL PROTECTED]> wrote: >> On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote: >> >> >> >> > I am trying to rewrite some C source code for a poker hand evaluator >> > in Python.  Putting aside all of the comments such as just using t

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
On Jul 10, 1:35 pm, MRAB <[EMAIL PROTECTED]> wrote: > On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote: > > > > > I am trying to rewrite some C source code for a poker hand evaluator > > in Python.  Putting aside all of the comments such as just using the C > > code, or using SWIG, etc.  I have

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread MRAB
On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote: > I am trying to rewrite some C source code for a poker hand evaluator > in Python.  Putting aside all of the comments such as just using the C > code, or using SWIG, etc.  I have been having problems with my Python > code not responding the sam

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
Well, I have figured out something that works: def findit(u): u += 0xe91aaa35 u1 = ~(0x - u) ^ u >> 16 u1 += ((u1 << 8) & 0x) u1 ^= (u1 & 0x) >> 4 b = (u1 >> 8) & 0x1ff a = (u1 + (u1 << 2) & 0x) >> 19 r = int(a) ^ hash_adjust[int(b)]

Re: error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Peter Otten
Jordan wrote: > C: > > u starts at 1050 > > u += 0xe91aaa35; > > u is now -384127409 Hm, a negative unsigned... > Python: > >    u starts at 1050 > >    u += 0xe91aaa35 > >    u is now  3910839887L Seriously, masking off the leading ones is the way to go: >>> -384127409 & 0x == 3

Re: error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Jordan
if after the first step (u += 0xe91aaa35) you apply this function: invert = lambda x: ~int(hex(0x - x)[0:-1],16) it returns the correct value (corrected the overflow) but there is still something wrong, still looking into it, if someone knows how to do this, feel free to comment :) -- ht

Re: error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Jordan
I realize I did a pretty bad job of explaining the problem. The problem is the python version is returning an r that is WY to big. Here is an example run through that function in each language: C: u starts at 1050 u += 0xe91aaa35; u is now -384127409 u ^= u >> 16; u

Re: error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Jordan
I was actually just going through an example to show what was happening each step of the way and noticed the overflow!!! bah, stupid tricks tricks tricks!!! The problem is def the overflow, I notice that I start to get negative numbers in the C version, which makes me think that the & 0x t

Re: error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Dan Stromberg
On Wed, 09 Jul 2008 20:56:59 -0700, Jordan wrote: > I am trying to rewrite some C source code for a poker hand evaluator in > Python. Putting aside all of the comments such as just using the C > code, or using SWIG, etc. I have been having problems with my Python > code not responding the same w