Binary to Decimal for >2**32

G

Graham Drabble

Hi all,

I have been using a binary to decimal converter for number <2**16 for
a while now using

$bin = sprintf("%016b",$dec)

which works fine.

I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
(everything >=2**32 returns the same as (2**32)-1 ).

I'm currently getting round this with

use strict;
use warnings;
use Math::BigInt;

my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
if ($num >= 2**32){
my $upper = int($num / (2**32));
my $lower = $num % 2**32;

$upper = sprintf ("%016b",$upper);
$lower = sprintf ("%032b",$lower);
print "$upper$lower\n";
}
else
{
printf ("%048b\n",$num);
}

Which works but seems very inelegant. Does anyone have a better
suggestion?

Also hex() returns a warning when given a hex string >2**32 but
returns the correct result. Is this something that can be relied on
or do I need to find a work around for that as well?
 
J

Jürgen Exner

Graham said:
I have been using a binary to decimal converter for number <2**16 for
a while now [...] which works fine.
I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
use Math::BigInt; [...]
Which works but seems very inelegant. Does anyone have a better
suggestion?

Use a perl that is compiled with 64-bit support on a 64-bit machine.

jue
 
B

Ben Morrow

Quoth "Jürgen Exner said:
Graham said:
I have been using a binary to decimal converter for number <2**16 for
a while now [...] which works fine.
I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
use Math::BigInt; [...]
Which works but seems very inelegant. Does anyone have a better
suggestion?

Use a perl that is compiled with 64-bit support on a 64-bit machine.

s/ on a 64-bit machine//;

~% perl -v

This is perl, v5.8.8 built for i386-freebsd-64int
<snip>

~% perl -le'print sprintf "%048b", 2**37'
000000000010000000000000000000000000000000000000

Ben
 
M

Mumia W.

Hi all,

I have been using a binary to decimal converter for number <2**16 for
a while now using

$bin = sprintf("%016b",$dec)

which works fine.

I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
(everything >=2**32 returns the same as (2**32)-1 ).

I'm currently getting round this with

use strict;
use warnings;
use Math::BigInt;

my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
if ($num >= 2**32){
my $upper = int($num / (2**32));
my $lower = $num % 2**32;

$upper = sprintf ("%016b",$upper);
$lower = sprintf ("%032b",$lower);
print "$upper$lower\n";
}
else
{
printf ("%048b\n",$num);
}

Which works but seems very inelegant. Does anyone have a better
suggestion?

Also hex() returns a warning when given a hex string >2**32 but
returns the correct result. Is this something that can be relied on
or do I need to find a work around for that as well?

Use the 'as_bin' method, e.g.:

my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
print $num->as_bin, "\n";

The output will be prefixed with "0b" which you can remove.
 
M

Mumia W.

[...]
Anyone got any advice regarding a substitute for hex()? Whilst it works
on all the environments I see I don't like to rely on something working
when the docs say it won't and the error message will worry users.
Unfortunately using 64bit Perl isn't an option.

my $num2 = Math::BigInt->new('0x8000c0000000');
print $num2, "\n";

:)

I hope I made your day a little brighter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top