Fun with Powers of Two


Recently I needed to know the maximum number of decimal digits in a 64-bit unsigned integer. I put together this handy table as a reference.

Value Decimal Decimal Digits
2^8 256 3
2^16 65,536 5
2^32 4,294,967,296 10
2^64 1.8 x 10^19 20
2^128 3.4 x 10^38 39
2^256 1.16 x 10^77 78

As the exponent doubles, the number of digits tends to double. If you remember than a 32-bit number has at most ten decimal digits, then it’s easy to remember that a 64-bit number has at most twenty decimal digits, and so on.

Here’s another useful trick involving powers of 1024.

n Power of 2 Decimal Think Decimal Digits
1 2^(10n) 1024 1 thousand (10^3) 3+1
2 2^20 1,048,576 1 million (10^6) 6+1
3 2^30 1,073,741,824 1 billion (10^9) 9+1
4 2^40 1.1 x 10^12 10^12 12+1
5 2^50 1.1 x 10^15 10^15 15+1
6 2^60 1.2 x 10^18 10^18 18+1
7 2^70 1.2 x 10^21 10^21 21+1
8 2^80 1.2 x 10^24 10^24 24+1
9 2^90 1.2 x 10^27 10^27 27+1
10 2^100 1.3 x 10^30 10^30 30+1
n 2^(10n) ~10^(3n) ~10^(3n) (3n)+1

Given a number that’s a power of 1024, i.e. 2^(10 x n), where n is positive integer, the maximum number of decimal digits in the equivalent value is (3 x n) + 1. For instance, for 2^20 = 2^(10 x 2) = 1,048,576, the maximum number of decimal digits is 7, because n is 2 and (3 x 2) + 1. This is true for all values of n through 97 (2^970), so this trick will be handy for a few years to come 🙂

Using this formula, here’s how you would quickly approximate the maximum number of decimal digits required for a 64-bit integer. Round up 64 to the nearest number divisible by 10 (70). Divide by 10 (7). Multiply by three and add 1 (7 x 3 + 1) = 22. Nice!

Advertisements
This entry was posted in C++, Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s