Friday, October 07, 2011

Quick way to tell how many bits a number takes to store.

To go from bit to number or number to bits you can use BC from the Unix/Linux command line.

BC has a few nice features one is unlimited precision arithmetic.  Another is you can pipe standard in and out from it and use is as a math engine in shell scripts, to quickly do BIGNUM math without getting in to any compiled code, alternate languages and unfamiliar libraries.

If you are using cygwin you may need to install bc first using setup.

What I typed is clear.
The response is in green.

From the Unix Shell:

[jsokol]$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

l(4294967296) / l(2)

This works for any base number too.

the "-l" option on bc enables floating point number math too, this is not unlimited precision but required for the "l()" log function to work.

No comments: