Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [25]
The number of teaspoons in a tablespoon is 3TEN or 11TWO
The number of sides to a square is 4TEN or 100TWO
The number of fingers on one human hand is 5TEN or 101TWO
The number of legs on an insect is 6TEN or 110TWO
The number of days in a week is 7TEN or 111TWO
The number of musicians in an octet is 8TEN or 1000TWO
The number of planets in our solar system (including Pluto) is 9TEN or 1001TWO
The number of gallons in a cowboy hat is 10TEN or 1010TWO
and so forth.
In a multidigit binary number, the positions of the digits correspond to powers of two:
So anytime we have a binary number composed of a 1 followed by all zeros, that number is a power of two. The power is the same as the number of zeros in the binary number. Here's our expanded table of the powers of two demonstrating this rule:
Power of Two
Decimal
Octal
Quaternary
Binary
2 0
1
1
1
1
2 1
2
2
2
10
2 2
4
4
10
100
2 3
8
10
20
1000
2 4
16
20
100
10000
2 5
32
40
200
100000
2 6
64
100
1000
1000000
2 7
128
200
2000
10000000
2 8
256
400
10000
100000000
2 9
512
1000
20000
1000000000
2 10
1024
2000
100000
10000000000
2 11
2048
4000
200000
100000000000
2 12
4096
10000
1000000
1000000000000
Let's say we have the binary number 101101011010. This can be written as
101101011010TWO = 1 x 2048TEN +
0 x 1024TEN +
1 x 512TEN +
1 x 256TEN +
0 x 128TEN +
1 x 64TEN +
0 x 32TEN +
1 x 16TEN +
1 x 8TEN +
0 x 4TEN +
1 x 2TEN +
0 x 1TEN
The same number can be written this way:
101101011010TWO = 1 x 211 +
0 x 210 +
1 x 29 +
1 x 28 +
0 x 27 +
1 x 26 +
0 x 25 +
1 x 24 +
1 x 23 +
0 x 22 +
1 x 21 +
0 x 20
If we just add up the parts in decimal, we get 2048 + 512 + 256 + 64 + 16 + 8 + 2, which is 2,906TEN.
To convert binary numbers to decimal more concisely, you might prefer a method that uses a template I've prepared:
This template allows you to convert numbers up to eight binary digits in length, but it could easily be extended. To use it, put up to eight binary digits in the 8 boxes at the top, one digit to a box. Do the eight multiplications and put the products in the 8 lower boxes. Add these eight boxes for the final result. This example shows how to find the decimal equivalent of 10010110:
Converting from decimal to binary isn't quite as straightforward, but here's a template that let's you convert decimal numbers from 0 through 255 to binary:
The conversion is actually trickier than it appears, so follow the directions carefully. Put the entire decimal number (less than or equal to 255) in the box in the upper left corner. Divide that number (the dividend) by the first divisor (128), as indicated. Put the quotient in the box below (the box at the lower left corner), and the remainder in the box to the right (the second box on the top row). That first remainder is the dividend for the next calculation, which uses a divisor of 64. Continue in the same manner through the template.
Keep in mind that each quotient will be either 0 or 1. If the dividend is less than the divisor, the quotient is 0 and the remainder is simply the dividend. If the dividend is greater than or equal to the divisor, the quotient is 1 and the remainder is the dividend minus the divisor. Here's how it's done with 150:
If you need to add or multiply two binary numbers, it's probably easier to do the calculation in binary rather than convert to decimal. This is the part you're really going to like. Imagine how quickly you could have mastered addition if the only thing you had to memorize was this:
+
0
1
0
0
1
1
1
10
Let's use this table to add two binary numbers:
Starting at the right column: 1 plus 0 equals 1. Second column from right: 0 plus 1 equals 1. Third column: 1 plus 1 equals 0, carry the 1. Fourth column: 1 (carried) plus 0 plus 0 equals 1. Fifth column: 0 plus 1 equals 1. Sixth column: 1 plus 1 equals 0, carry the 1. Seventh column: 1 (carried) plus 1 plus 0 equals 10.
The multiplication table