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


In reply to help porting a line of bitwise C code to perl by blahblahblah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.