Multiplying by Twos

See all articles from Sinc-Link v11 n3
Author(s): Rene Bruneau
Publication: Sinc-Link
Pub Details: Sinc-Link v11 n3 (Volume 11, Issue 3)
Date: May/June 1993
Pages: 6

Here is a quick programming trick that I learned to convert binary numbers to decimal. Starting from the most significant bit (MSB) – the one on the extreme left – multiply it by 2 and add the result to the bit to the right, then multiply the new result by 2 and add to the bit to the right, continuing the process untill there are no more bits to process.

Example:   1001010 = 74    1 * 2 + 0 =  2
                           2 * 2 + 0 =  4
                           4 * 2 + 1 =  9
                           9 * 2 + 0 = 18
                          18 * 2 + 1 = 37
                          37 * 2 + 0 = 74

Because you double and add to the right, the arithmetic is simple and can be done in your head – no need to go looking for a calculator or conversion tables. Besides you can always impress your friends and fellow programmers with your computer-like brain — maybe –.