blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to port a bit of old C code to perl. I generated a ton of test cases from the C program output, wrote my Perl code, and then compared the results for the same inputs. Some cases are successful; others are not. I put in a bunch of debugging and fixed some simple things -- incorrect array indexes; for loops translated wrong; etc. -- but I'm hung up on something I can't figure out.
Here is the C code:
In my failing case, the values of token4 & token5 are 0 and 32, resulting in:return (func(token4) & 95) | (func(token5) & 32 | func(token5) & 128); /* "func" is defined as: */ unsigned long func (c) unsigned char *c;
The C code evaluates this to a result of zero. I tried porting the same to perl, and for some of my test cases it matches the C result, but for this particular case the following perl code prints 32:(0 & 95) | (32 & 32) | (32 & 128)
I read perlop but didn't find anything meaningful to me. It seems from Will/Can Perl 6 clean up the bit-wise operator precedence mess? that Perl tries to have the same operator precedence as C. Do I need to pack my data into something other than a plain old scalar? I'm not well versed in C or in this sort of bitwise manipulation in Perl. Can anyone spot an obvious problem or offer some pointers?perl -e 'print ((0 & 95) | (32 & 32) | (32 & 128))'
Thanks,
Joe
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: help porting a line of bitwise C code to perl
by dsheroh (Monsignor) on Mar 07, 2008 at 05:03 UTC | |
Re: help porting a line of bitwise C code to perl
by BrowserUk (Patriarch) on Mar 07, 2008 at 05:32 UTC | |
by missingthepoint (Friar) on Sep 05, 2008 at 12:26 UTC | |
by BrowserUk (Patriarch) on Sep 05, 2008 at 16:23 UTC | |
Re: help porting a line of bitwise C code to perl
by Anonymous Monk on Mar 07, 2008 at 04:42 UTC | |
Re: help porting a line of bitwise C code to perl
by blahblahblah (Priest) on Mar 07, 2008 at 05:19 UTC |
Back to
Seekers of Perl Wisdom