in reply to dec to bin conversion for large numbers
Not very portable but some fun with Inline on I386 Linux:
-derby#!/usr/local/bin/perl use Inline C; print dec2bin(13) . "\n"; print idec2bin(8589934593) . "\n"; sub dec2bin { return ( sprintf "%b", shift ); } __END__ __C__ void idec2bin ( SV *num ) { long long count; long long MASK; long long var; char *beg; char *end; count = sizeof( var ) * 8; MASK = 1LL << (count-1); beg = SvPV( num, PL_na ); var = strtoll( beg, &end, 10 ); while( count-- ) { printf( "%d", (var & MASK) ? 1 : 0 ); var <<= 1; } }
|
|---|