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:
return (func(token4) & 95) | (func(token5) & 32
| func(token5) & 128);
/* "func" is defined as: */
unsigned long func (c) unsigned char *c;
In my failing case, the values of token4 & token5 are 0 and 32, resulting in:
(0 & 95) | (32 & 32) | (32 & 128)
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:
perl -e 'print ((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?
Thanks,
Joe
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|