Binary to Decimal for >2**32
Binary to Decimal for >2**32
am 14.11.2007 21:45:57 von 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?
--
Graham Drabble
http://www.drabble.me.uk/
Re: Binary to Decimal for >2**32
am 14.11.2007 23:06:43 von jurgenex
Graham Drabble wrote:
> 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
Re: Binary to Decimal for >2**32
am 15.11.2007 01:19:19 von Ben Morrow
Quoth "Jürgen Exner" :
> Graham Drabble wrote:
> > 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
~% perl -le'print sprintf "%048b", 2**37'
000000000010000000000000000000000000000000000000
Ben
Re: Binary to Decimal for >2**32
am 15.11.2007 16:22:58 von paduille.4061.mumia.w+nospam
On 11/14/2007 02:45 PM, Graham Drabble wrote:
> 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.
Re: Binary to Decimal for >2**32
am 15.11.2007 17:40:36 von Graham Drabble
On 15 Nov 2007 "Mumia W."
wrote in
news:13josnthp90su07@corp.supernews.com:
> 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.
D'oh. Very much simpler thanks. Need to pad it out to 48bits as well
but that's trivial.
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.
--
Graham Drabble
http://www.drabble.me.uk/
Re: Binary to Decimal for >2**32
am 15.11.2007 19:15:21 von paduille.4061.mumia.w+nospam
On 11/15/2007 10:40 AM, Graham Drabble wrote:
> [...]
> 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.