We don't bite newbies here... much PerlMonks

### Re: Converting 2**54-1 to Binary

by duelafn (Parson)
 on Mar 18, 2017 at 13:55 UTC ( #1185143=note: print w/replies, xml ) Need Help??

in reply to Converting 2**54-1 to Binary

And, to make it work as you were expecting, use Math::BigInt

```use 5.016;
use Test::More qw(no_plan);
use Math::BigInt;

for (51..66) {
# substr to remove 0b prefix
my \$s = substr(Math::BigInt->new("2")->bpow(\$_)->bdec->as_bin, 2);
# Or: Math::BigInt->new("1")->blsft(\$_)->bdec->as_bin
is length(\$s), \$_, "1 << \$_ -1";
}
__END__
ok 1 - 1 << 51 -1
ok 2 - 1 << 52 -1
ok 3 - 1 << 53 -1
ok 4 - 1 << 54 -1
ok 5 - 1 << 55 -1
ok 6 - 1 << 56 -1
ok 7 - 1 << 57 -1
ok 8 - 1 << 58 -1
ok 9 - 1 << 59 -1
ok 10 - 1 << 60 -1
ok 11 - 1 << 61 -1
ok 12 - 1 << 62 -1
ok 13 - 1 << 63 -1
ok 14 - 1 << 64 -1
ok 15 - 1 << 65 -1
ok 16 - 1 << 66 -1
1..16

Good Day,
Dean

Replies are listed 'Best First'.
Re^2: Converting 2**54-1 to Binary
by swl (Vicar) on Mar 18, 2017 at 23:58 UTC

Or Math::BigNum if one has a large number of calculations and needs a bit more speed.

```use 5.016;
use Test::More qw(no_plan);
use Math::BigNum;

for (51..66) {
# no need to substr to remove 0b prefix
my \$s = Math::BigNum->new(2)->bpow(\$_)->bdec->as_bin;
is length(\$s), \$_, "1 << \$_ -1";
}
__END__
ok 1 - 1 << 51 -1
ok 2 - 1 << 52 -1
ok 3 - 1 << 53 -1
ok 4 - 1 << 54 -1
ok 5 - 1 << 55 -1
ok 6 - 1 << 56 -1
ok 7 - 1 << 57 -1
ok 8 - 1 << 58 -1
ok 9 - 1 << 59 -1
ok 10 - 1 << 60 -1
ok 11 - 1 << 61 -1
ok 12 - 1 << 62 -1
ok 13 - 1 << 63 -1
ok 14 - 1 << 64 -1
ok 15 - 1 << 65 -1
ok 16 - 1 << 66 -1
1..16

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1185143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2022-12-07 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?