Computer Science › Section B · Data storage › Topic B.5
The binary system
Every number a computer handles is ultimately a row of 0s and 1s. Reading, writing and adding those rows by hand is the skill that makes integers, fractions and every later data format make sense.
Place value with a different base
In ordinary decimal notation each digit's position carries a weight, and each weight is ten times the one on its right. The numeral 604 means \(6 \times 10^2 + 0 \times 10^1 + 4 \times 10^0\). Binary notation, also called base two, uses the same scheme with two changes: there are only two digits, 0 and 1, and each position's weight is twice the one to its right. Reading from the right, the weights are 1, 2, 4, 8, 16, 32, 64, 128 and so on, the powers of 2. Since each bit has two states, a row of bits reads directly as a binary numeral. When the base could be unclear, a subscript shows it: \(101_2\) is five, while \(101_{10}\) is one hundred and one.
Reading a binary numeral
To find the value of a binary numeral, multiply each digit by its position's weight and add. Because every digit is 0 or 1, this reduces to adding the weights wherever a 1 appears. For 110110, label the positions from the right with weights 1, 2, 4, 8, 16 and 32. The 1s sit under 32, 16, 4 and 2, so the value is \(32 + 16 + 4 + 2 = 54\). As a check, a binary numeral ends in 0 exactly when its value is even, since every weight except 1 is even. Also, n bits can show only 0 to \(2^n - 1\), so six bits never exceed 63.
From decimal to binary: subtract powers of two
One way to convert a decimal whole number is to peel off powers of two from the largest down. List the weights up to the number, then work from the left: if the weight fits into what remains, write 1 and subtract it; if it does not, write 0. For 54: 32 fits (22 left), 16 fits (6 left), 8 does not fit, 4 fits (2 left), 2 fits (0 left) and 1 does not fit. The digits, in order, are 110110, matching the previous card. This method suits small numbers, whose powers of two are easy to hold in your head.
Worked example: repeated division by two
For larger numbers, divide by 2, record the remainder, and keep dividing each new quotient until it reaches 0. Convert 89. 1. 89 ÷ 2 = 44, remainder 1. 2. 44 ÷ 2 = 22, remainder 0. 3. 22 ÷ 2 = 11, remainder 0. 4. 11 ÷ 2 = 5, remainder 1. 5. 5 ÷ 2 = 2, remainder 1. 6. 2 ÷ 2 = 1, remainder 0. 7. 1 ÷ 2 = 0, remainder 1, so stop. 8. Read the remainders from the last one back to the first: 1011001. Check: \(64 + 16 + 8 + 1 = 89\). The first remainder becomes the rightmost digit because it records whether 89 is odd.
Adding in binary
Binary addition uses the decimal column method: work from right to left, write the low digit of each column's sum, and carry the rest. Only four facts are needed: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10, which means write 0 and carry 1. With a carry coming in, a column can hold three 1s, and 1 + 1 + 1 = 11: write 1 and carry 1. Nothing else can happen in a column, so a small adding circuit can repeat the same job for every bit position.
Worked example: adding two binary numbers
Add 101101 and 11110, which are 45 and 30. Line them up on the right, writing the second as 011110. 1. First column: 1 + 0 = 1, write 1. 2. Second: 0 + 1 = 1, write 1. 3. Third: 1 + 1 = 10, write 0, carry 1. 4. Fourth: 1 + 1 plus the carry is 11, write 1, carry 1. 5. Fifth: 0 + 1 plus the carry is 10, write 0, carry 1. 6. Sixth: 1 + 0 plus the carry is 10, write 0, carry 1. 7. The final carry becomes a new leftmost digit. The sum is 1001011. Check: \(64 + 8 + 2 + 1 = 75\), and 45 + 30 = 75. The sum needs seven bits, although both inputs fit in six.
Fractions and the radix point
A dot called the radix point, the binary version of the decimal point, separates the whole part from the fractional part. Positions to the right of it have weights \(\frac{1}{2}\), \(\frac{1}{4}\), \(\frac{1}{8}\), \(\frac{1}{16}\) and so on, each still half the weight on its left. So 110.011 means \(4 + 2 + \frac{1}{4} + \frac{1}{8}\), which is \(6\frac{3}{8}\) or 6.375. Adding numerals that have radix points works exactly as before once the points are lined up: 1.1 + 10.01 = 11.11, which in decimal is 1.5 + 2.25 = 3.75.
Worked example: a decimal fraction in binary
To convert a fraction between 0 and 1, double it repeatedly. Each whole-number part (0 or 1) is the next digit after the radix point; keep only the fractional part. Convert 0.6875. 1. 0.6875 × 2 = 1.375: digit 1, keep 0.375. 2. 0.375 × 2 = 0.75: digit 0. 3. 0.75 × 2 = 1.5: digit 1, keep 0.5. 4. 0.5 × 2 = 1.0: digit 1, nothing left, so stop. 5. The answer is 0.1011. Check: \(\frac{1}{2} + \frac{1}{8} + \frac{1}{16} = \frac{11}{16} = 0.6875\). Now try 0.1: the doubling gives 0, 0, 0, 1, 1, 0, 0, 1, 1, and after the first digit the block 0011 repeats for ever. One tenth has no exact binary form, just as one third has no exact decimal form.
Hexadecimal as a number system
The topic "Bits, gates, and flip-flops" introduced hexadecimal as shorthand for bit patterns: group the bits in fours from the right and write each group as one digit, 0 to 9 then A to F for ten to fifteen. Place value explains why this works. Hexadecimal is base 16, with weights 1, 16, 256 and so on, and since \(16 = 2^4\), one hex digit covers exactly four binary places. So 1011 0110 is 0xB6, whose value is \(11 \times 16 + 6 = 182\), the same as the binary weights give: 128 + 32 + 16 + 4 + 2. Web colour codes use hex too: #FF0000 sets red to FF, the largest byte value, 255, and green and blue to 0.
Common mistakes to avoid
Reading the division remainders in the order they were found reverses the answer: 89 would come out as 1001101, which is 77. Weights are counted from the right end, never the left. In addition, the column with three 1s is the one people miss: it gives 1 with a carry, not 0. When converting to hex, group from the right and pad the leftmost group with 0s, so 101101 is 10 1101, then 0010 1101, which is 2D. Finally, do not assume a short decimal fraction is short in binary; 0.1 never ends.
Key takeaways
Binary is place-value notation with powers of 2 as weights; a numeral's value is the sum of the weights under its 1s. Convert decimal to binary by subtracting powers of two or by repeated division, reading remainders from last to first. Add column by column using 1 + 1 = 10 and 1 + 1 + 1 = 11. Positions after the radix point weigh one half, one quarter, one eighth and so on; repeated doubling converts a decimal fraction, and some, such as 0.1, repeat for ever. Hexadecimal is base 16, with weights 1, 16, 256, so each digit packs four bits.
Flashcards
Flashcard
1, 2, 4, 8, 16, 32, 64, 128 ...
Test your understanding
Question 1 of 5
What is the binary numeral 1011010 in decimal?
Question 2 of 5
Converting 43 by repeated division gives the remainders 1, 1, 0, 1, 0, 1 in the order they were found. What is 43 in binary?
Question 3 of 5
What is 1011 + 111 in binary?
Question 4 of 5
Which of these decimal values cannot be written exactly as a binary fraction with a finite number of digits?
Question 5 of 5
What is the binary numeral 1111010 written in hexadecimal?
