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

error when porting C code to Python (bitwise manipulation)

2008-07-09 Thread Jordan
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 way as the C version. C verison: unsigned find_fast(unsign

Re: porting C code

2005-01-15 Thread Lucas Raab
Peter Hansen wrote: Lucas Raab wrote: Sorry, the third "byte" is what I meant. Fair enough. Note, however, that as someone pointed out, it's actually the *fourth* of something, and it would not necessarily be a byte. In fact, in your case, it's not: typedef unsigned long int word32 ; void mu

Re: porting C code

2005-01-14 Thread Peter Hansen
Lucas Raab wrote: Sorry, the third "byte" is what I meant. Fair enough. Note, however, that as someone pointed out, it's actually the *fourth* of something, and it would not necessarily be a byte. In fact, in your case, it's not: typedef unsigned long int word32 ; void mu(word32 *a) { int i ;

Re: porting C code

2005-01-14 Thread Duncan Booth
Lucas Raab wrote: > Sorry, the third "byte" is what I meant. As for code samples, I hope the > following will work: > > typedef unsigned long int word32 ; > void mu(word32 *a) > { > int i ; > word32 b[3] ; > > b[0] = b[1] = b[2] = 0 ; > for( i=0 ; i<32 ; i++ ) > { > b[0] <<= 1 ; b[1]

Re: porting C code

2005-01-14 Thread Lucas Raab
Peter Hansen wrote: Lucas Raab wrote: I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer. If that's really exactly what you have, then you actually have something defining an array of three unsigned long integers named

Re: porting C code

2005-01-13 Thread Peter Hansen
Peter Hansen wrote: but merely a "b[3]" reference somewhere, it would be referencing the third element of an array called "b", which is possibly a byte, "*Fourth* element... I'll come in again. Amongst our elements..." -Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: porting C code

2005-01-13 Thread Peter Hansen
Lucas Raab wrote: I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing the third bit of the integer. If that's really exactly what you have, then you actually have something defining an array of three unsigned long integers named "b". And even if y

Re: porting C code

2005-01-13 Thread Roy Smith
Lucas Raab <[EMAIL PROTECTED]> wrote: > I am currently in the process of porting some C code into Python and am > stuck. I don't claim to be the greatest C/C++ programmer; in fact, my > skills at C are rudimentary at best. My question is I have the > statement: "typedef unsigned long int word

Re: porting C code

2005-01-13 Thread Steven Bethard
Lucas Raab wrote: I am currently in the process of porting some C code into Python and am stuck. I don't claim to be the greatest C/C++ programmer; in fact, my skills at C are rudimentary at best. My question is I have the statement: "typedef unsigned long int word32" and later on: "word32 b

porting C code

2005-01-13 Thread Lucas Raab
I am currently in the process of porting some C code into Python and am stuck. I don't claim to be the greatest C/C++ programmer; in fact, my skills at C are rudimentary at best. My question is I have the statement: "typedef unsigned long int word32" and later on: "word32 b[3]" referencing t