Can somebody explain to me how to do 32-bit operations in Perl?
I wouldn't be very comfortable doing 32-bit unsigned integer operations in perl if overflows can occur. I'd probably do it in C and access the operations from perl via Inline::C or XS.
As already mentioned, 'use integer', is probably of value though I wouldn't like to vouch for its reliability - especially on a perl version as old as 5.8.
I don't know what "TinyPerl" is - could you provide the output of
perl -V (that's an uppercase "V").
But on the first line, I get -1, and on the second line I get the positive number in base 10
As you've noted, "-1" and "4294967925" are the same 32-bit integer.
On the first line you asked for it to be expressed as a signed value (ie -1). On the second line you got to see the unsigned value (4294967295).
It seems to me that since you're actually interested in the unsigned values, the first line should have specified "%u" instead of "%d".
But, of course, the value you really expected to see was 855638016, for which you need to satisfy 2 requirements:
1) That you 'use integer'
2) That the size of your perl's integer is 4 bytes (check
perl -V:ivsize).
With both of those conditions satisfied I get:
LINE 1: 855638016 == 855638016
LINE 2: 855638016 == 855638016
But if perl's integer is 8 bytes then, irrespective of whether I 'use integer', I get:
LINE 1: 855638016 == 5150605312
LINE 2: 855638016 == 855638016
Note that, in this instance, the "%d" in "LINE 1" is providing a signed 64-bit value because the perl integer is a 64-bit type.
Cheers,
Rob
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.