Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to convert decimal integers into binary
and was using the following code
By repsonprint dec2bin(13) . "\n"; sub dec2bin { return (sprintf "%b",shift); }
but they don't work for numbers > 32 bits, is there an easy way to do this?sub to_bin { my $num = shift; my $ret = ''; while ($num){ $ret = (($num % 2) ? 1 : 0); $num >>= 1; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: dec to bin conversion for large numbers
by tilly (Archbishop) on Sep 14, 2001 at 18:29 UTC | |
|
(tye)Re: dec to bin conversion for large numbers
by tye (Sage) on Sep 14, 2001 at 21:21 UTC | |
|
Re: dec to bin conversion for large numbers
by I0 (Priest) on Sep 15, 2001 at 12:26 UTC | |
|
Re: dec to bin conversion for large numbers
by claree0 (Hermit) on Sep 14, 2001 at 17:55 UTC | |
by jlongino (Parson) on Sep 14, 2001 at 18:36 UTC | |
|
Re: dec to bin conversion for large numbers
by derby (Abbot) on Sep 15, 2001 at 05:15 UTC | |
|
Re: dec to bin conversion for large numbers
by chiller (Scribe) on Sep 14, 2001 at 21:42 UTC | |
by chiller (Scribe) on Sep 14, 2001 at 21:51 UTC |