in reply to Bitshift limits
Here's an Inline::BC solution:
use strict; use warnings; use Inline 'BC'; my @nums = qw /0 1 5 10 16 2147483647 9223372036854775807 124897514814098457204985981709857243095827435/; foreach my $number (@nums) { print "$number: ", np ($number); } __DATA__ __BC__ define np (i) { auto j; while (i) { i /= 2; j += 1; } return (2 ^ j) } 0: 1 1: 2 5: 8 10: 16 16: 32 2147483647: 2147483648 9223372036854775807: 9223372036854775808 124897514814098457204985981709857243095827435: 17840596158824498513228 +5746181186892047843328
And here's a Math::BigInt one:
use strict; use warnings; use Math::BigInt; my @nums = qw /0 1 5 10 16 2147483647 9223372036854775807 124897514814098457204985981709857243095827435/; foreach my $number (@nums) { $number = Math::BigInt -> new ($number); my $result = Math::BigInt -> new (1); print "$number: "; until ($number == 0) { $number /= 2; $result *= 2; } print "$result\n"; } __END__ 0: 1 1: 2 5: 8 10: 16 16: 32 2147483647: 2147483648 9223372036854775807: 9223372036854775808 124897514814098457204985981709857243095827435: 17840596158824498513228 +5746181186892047843328
Abigail
|
|---|