0

I want to print a 64-bit number into an other variable.

#!/bin/perl

use strict;
use bigint;

my $str         = "";
my $long_number = -1;

$str = sprintf("%016X", $long_number);

But when I print $str I see 00000000ffffffff.

It's like Perl considers my $long_number to be a 32-bit number.

I checked my Perl version. It looks correct :

perl --version

This is perl, v5.8.4 built for sun4-solaris-64int (with 37 registered patches, see perl -V for more detail

Does anyone know where my problem comes from ?

Borodin
  • 126,100
  • 9
  • 70
  • 144
LnlB
  • 180
  • 2
  • 11

2 Answers2

2

Although you're running on 64-bit Solaris, likely you have a 32-bit int Perl.

You could run perl -V (uppercase V) and check for compile-time options:

Characteristics of this binary (from libperl):
  Compile-time options: PERL_MALLOC_WRAP PERL_USE_SAFE_PUTENV
                        *USE_64_BIT_INT* USE_LARGE_FILES USE_PERLIO

Or perl -V:ivsize; size 8 indicates 64-bit, 4 32-bit.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
2

Your Perl does indeed use 64-bit integers as hinted by perl -v. This is confirmed by perl -V:ivsize returning 8.

The problem relates to use bigint;. Using that causes your -1 to be replaced with Math::BigInt->new(-1), and the the numification of that object is what you're printing. I bet it's that object's numification returns the 32-bit representation of -1.

Removing use bigint; should do the trick.

ikegami
  • 367,544
  • 15
  • 269
  • 518