I'm having some trouble manipulating 64-bit values in Perl. `perl -V` confirms that I am running perl 5.6.1 with USE_64_BIT_INT and USE_64_BIT_ALL on Tru64 UNIX. I believe what I am seeing is that whenever I do a comparison or any arithmetic, perl with translate the value to a floating point number, losing the precision that I need. (Bit manipulation operators are okay)

As a work around, I wind up manipulating the number as two 32-bit quantities. However, this is much slower, and, IMNSHO, quite ridiculous.

Below is a script and it's output illustrating what I am describing:

$ ./foo.pl A) 0xfffffc0123456789 (should be 0xfffffc0123456789) B) 0xfffffc0123456800 (should be 0xfffffc0123456789) C) 0xfffffc0123456789 (should be 0xfffffc0123456789) D) 0xfffffc0123456800 (should be 0xfffffc0123456788 or maybe 0xfffffc0 +123456790) E) 0x23456789 (should be 0x23456789) F) 0xfffffc01 (should be 0xfffffc01) G) Comparing 0xfffffc0123456789 to 0xfffffc0123456785: 1) 0 (should be 1) 2) 0 (should be -1) 3) 0 (should be 0) $ $ cat foo.pl #!/usr/local/bin/perl $a = 0xfffffc0123456789; printf "A) %#x (should be 0xfffffc0123456789)\n", $a; $str = "0xfffffc0123456789"; printf "B) %#x (should be 0xfffffc0123456789)\n", hex($str); $b = unpack "Q", (pack "L2", hex(substr($str, 10)), hex(substr($str, 2 +, 8))); printf "C) %#x (should be 0xfffffc0123456789)\n", $b; printf "D) %#x (should be 0xfffffc0123456788 or maybe 0xfffffc01234567 +90)\n", $a - 1; printf "E) %#x (should be 0x23456789)\n", $a & 0xffffffff; printf "F) %#x (should be 0xfffffc01)\n", $a >> 32; $b = 0xfffffc0123456785; printf "G) Comparing %#x to %#x:\n", $a, $b; printf " 1) %d (should be 1)\n", $a <=> $b; printf " 2) %d (should be -1)\n", $b <=> $a; printf " 3) %d (should be 0)\n", $a <=> $a; $

In reply to Manipulating 64-bit values by rjbell4

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.