University of Wales Trinity Saint David
ACCA4021 - Introduction to Computer Systems

Binary Numbers

Overview

Every value a computer stores, from a character in a document to a pixel on the screen, is held as a pattern of bits. This worksheet starts with how a group of bits is read as a whole number, how to convert between binary and the denary numbers you use every day, and how to add two binary numbers by hand. It then goes down a level, to how an electrical signal on a wire becomes a 1 or a 0 in the first place, and finishes with how those 1s and 0s can stand for true and false in logic. Work through the tasks in order, because each one uses ideas from the one before it. Most tasks have an interactive diagram or self-checking questions, so try each question yourself before checking.

Learning Objectives

By the end of this lab you will be able to:

Concept

A computer is built from millions of transistors, tiny switches that are either off or on. Inside the circuit, off and on show up as a low or a high voltage on a wire. A receiving circuit checks the wire at regular moments and compares the voltage with a threshold. Anything above it counts as 1 and anything below counts as 0. Each of those 0 or 1 readings is a bit, short for binary digit, and a group of eight bits is a byte.

Groups of bits can stand for numbers. Binary works on place value, exactly like the denary numbers you already use, except that each column is worth two times the column to its right (1, 2, 4, 8, 16 and so on) instead of ten times. The rightmost bit is the least significant bit and the leftmost is the most significant bit. Adding in binary is column addition, with a carry whenever a column reaches two.

Bits can also stand for answers, since a single bit is the natural way to hold the answer to a yes or no question. The branch of mathematics that works with values that can only be true or false is called Boolean logic. Writing true as 1 and false as 0 lets a computer store and combine answers using a small set of Boolean operations, mainly NOT, AND and OR.

Tasks

Task 1 - Place value in binary

An 8-bit binary number has eight columns. Each column is a power of two, starting from 20 = 1 on the right.

27262524232221201286432168421\begin{array}{cccccccc} 2^7 & 2^6 & 2^5 & 2^4 & 2^3 & 2^2 & 2^1 & 2^0 \\ 128 & 64 & 32 & 16 & 8 & 4 & 2 & 1 \end{array}

To find the denary value of a binary number, add up the place values of every column that holds a 1. For example, 00101101 has 1s in the 32, 8, 4 and 1 columns.

001011012=32+8+4+1=451000101101_2 = 32 + 8 + 4 + 1 = 45_{10}

The small subscript shows the base. A subscript 2 means binary and a subscript 10 means denary. Use the diagram below to explore. Click any bit to flip it and watch the denary value change.

  1. Set only the rightmost bit to 1. What value do you get?
  2. Now set every bit to 1. This is the largest number 8 bits can hold.
  3. Set the bits to make 100. Which columns did you need?
  4. Click 16-bit, then All 1s. What is the largest number 16 bits can hold?
  5. Try 32-bit and 64-bit as well. The line under the value shows how many different values each width can hold. Notice how quickly it grows, because every extra bit doubles it.

Computers usually work with numbers in these sizes. A 32-bit or 64-bit processor is one whose registers, the small stores it calculates with, hold 32 or 64 bits at a time.

What is the largest denary number that can be stored in 8 bits?

With every bit set to 1 the total is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. There are 256 different patterns, but one of them is 0, so the largest value is 255.

How many different values can be represented with 4 bits?

Each extra bit doubles the number of patterns, so 4 bits give 2 x 2 x 2 x 2 = 16 patterns, covering the values 0 to 15.

What is the largest denary number that can be stored in 16 bits?

16 bits give 216 = 65,536 different patterns. One of them is 0, so the largest value is 65,535. In general, n bits hold values from 0 to 2n - 1.

Task 2 - Convert binary to denary

Convert each binary number to denary. Write the column headings (128, 64, 32, 16, 8, 4, 2, 1) above the bits on paper if it helps, then add up the columns that contain a 1.

00000101 =

00001010 =

00011111 =

01000001 =

10010110 =

11001000 =

Look at your answers for 00000101 and 00001010. The second pattern is the first one shifted one place to the left.

What happened to the value when every bit moved one place to the left? Explain why using place value.

The value doubled, from 5 to 10. Each bit moved into a column worth twice as much as the one it came from, so every part of the total doubled, and therefore the whole total doubled. Shifting left by one place in binary multiplies by 2, in the same way that shifting left in denary multiplies by 10.

Task 3 - Convert denary to binary

To go the other way, work from the largest column down. At each column, ask whether the column value fits into what is left. If it fits, write a 1 and subtract it. If it does not fit, write a 0 and move on.

Worked example, converting 77:

ColumnFits into what is left?BitLeft over
128No (77)077
64Yes113
32No013
16No013
8Yes15
4Yes11
2No01
1Yes10

Reading the bit column from top to bottom gives 01001101. Check it by converting back: 64 + 8 + 4 + 1 = 77.

Convert each number below to 8-bit binary. Include leading zeros so that every answer is exactly 8 bits long.

9 =

20 =

63 =

100 =

128 =

250 =

Without converting it, how can you tell whether a binary number is odd or even?

Every column except the rightmost is worth an even amount (2, 4, 8 and so on). Only the 1 column can make the total odd, so the least significant bit decides it.

Task 4 - Binary addition

Binary addition works column by column from right to left, just like column addition in denary. There are only four rules to learn.

0+0=00+1=11+0=11+1=102(write 0, carry 1)\begin{array}{rcl} 0 + 0 &=& 0 \\ 0 + 1 &=& 1 \\ 1 + 0 &=& 1 \\ 1 + 1 &=& 10_2 \quad \text{(write 0, carry 1)} \end{array}

The last rule is the important one. One plus one is two, and two in binary is 10. So you write 0 in the current column and carry 1 into the next column to the left. When a column contains 1 + 1 + a carried 1, the total is three, which is 11 in binary. Write 1 and carry 1.

Worked example, 0101 + 0011 (5 + 3):

  carry   1 1 1
          0 1 0 1
        + 0 0 1 1
        ---------
          1 0 0 0

The result 1000 is 8, which matches 5 + 3. Now try these 4-bit additions. Give each answer as 4 bits, and check your result by converting to denary.

0010 + 0001 =

0110 + 0001 =

0011 + 0001 =

0101 + 0101 =

0111 + 0011 =

0110 + 0111 =

What is 1111 + 0001 if the answer must fit in 4 bits?

The true answer is 16, which is 10000 in binary and needs 5 bits. With only 4 bits available, the final carry has nowhere to go and the stored result is 0000. This is called overflow.

Task 5 - From signals to bits

Inside a computer, a bit travels along a wire as a voltage. In a simple circuit that runs between 0 V and 5 V, a 1 might be sent as about 4.5 V and a 0 as about 0.5 V. The receiving circuit does not try to measure the exact voltage. It only asks one question, is the voltage above the threshold of 2.5 V or below it?

A clock inside the computer ticks at a steady rate, and on each tick the receiver takes one sample of the wire. Eight samples in a row give eight bits, which is one byte.

Real wires are never perfectly clean. Nearby wires, heat and radio signals all add small random changes to the voltage, called noise. The diagram below shows a byte being sent along a wire. The navy line is the voltage, the dashed gold line is the threshold, and each circle is the moment the receiver samples the wire. Gold circles are read as 1 and white circles as 0.

  1. With the noise at 0 V, read the eight bits from the signal yourself and convert them to denary, using the method from Task 2. Check your bits against the Sent row and your value against the received byte.
  2. Click Send a new byte a few times and watch how the signal shape follows the bits.
  3. Slowly drag the noise slider up. Note roughly how much noise the signal can take before a bit is misread. Misread bits are shown in red. When one appears, look at how much the received denary value has changed because of a single wrong bit.

This is why computers use binary. With only two levels, a 1 sits 2 V above the threshold and a 0 sits 2 V below it, so the signal can wander a long way before it is misread. Now imagine using ten voltage levels in the same 0 V to 5 V range, one for each denary digit 0 to 9. Neighbouring levels would be only about 0.5 V apart, so a quarter of a volt of noise could turn a 6 into a 7. Two levels are far more reliable, and they are also simple to build, because a transistor is naturally either off or on.

A pattern of bits has no meaning on its own. The byte 01000001 could be the whole number 65, the letter A in a text file, or eight separate on and off settings. Its meaning depends on how a program chooses to read it. So far this worksheet has read every byte as a whole number. The next task looks at a different reading, bits as true and false.

In a 0 V to 5 V circuit with a 2.5 V threshold, the receiver samples the wire and measures 3.1 V. Which bit does it read?

3.1 V is above the 2.5 V threshold, so it is read as 1. The receiver only cares which side of the threshold the voltage is on, not its exact value.

What is the main reason computers use two voltage levels instead of ten?

With two levels the gap between a 0, the threshold and a 1 is large, so noise rarely pushes a reading to the wrong side. Binary numbers are actually longer than denary ones, but the reliability is worth it.

Using the diagram, roughly how much noise could the signal take before bits started to be misread? Explain why it is about that value.

About 2 V. A 1 is sent at 4.5 V and a 0 at 0.5 V, and both are 2 V away from the 2.5 V threshold. Noise has to push a sample more than 2 V in the wrong direction before it crosses the threshold, so below that level every bit is read correctly.

Task 6 - Bits as true and false

Logic is the study of reasoning with statements that are either true or false. In 1854 the mathematician George Boole showed that this kind of reasoning could be written as algebra, using values that can only ever be true or false. These are now called Boolean values.

A bit is a perfect fit for a Boolean value. Write true as 1 and false as 0, and one bit can store the answer to any yes or no question. Is a key being pressed? Is the number negative? Is the door open? Programs make decisions by combining answers like these with three Boolean operations.

  • NOT works on one value and flips it. NOT true is false, and NOT false is true.
  • AND combines two values and is true only when both are true. "I can go out if I have finished my work AND it is not raining."
  • OR combines two values and is true when at least one of them is true. "The alarm sounds if the door opens OR the window opens."

Using 1 for true and 0 for false, every possible result fits in a small table.

ABNOT AA AND BA OR B
00100
01101
10001
11011

Because a byte is just eight bits side by side, the same operations can be applied to two whole bytes one column at a time. These are called bitwise operations. Each column is worked out on its own, with no carries between columns.

        1 1 0 0 1 0 1 0
  AND   1 1 1 1 0 0 0 0
        ---------------
        1 1 0 0 0 0 0 0

Notice what the second byte did. Wherever it had a 1, the first byte's bit passed through unchanged, and wherever it had a 0, the result was forced to 0. A byte used like this is called a mask, because it hides the bits you do not want. This one keeps the left four bits and clears the right four.

Work out each result. Give single bits as 0 or 1, and bytes as all 8 bits.

NOT 1 =

1 AND 0 =

1 OR 0 =

0 OR 0 =

NOT 10110000 =

10101010 AND 11110000 =

10100000 OR 00000101 =

A game lets the player through a door only if they are holding the key and have defeated the guard. Which operation combines the two conditions?

Both conditions must be true at the same time, which is exactly what AND means.

A = 1 and B = 0. Which expression is true (equal to 1)?

A OR B is 1 because A is 1. A AND B needs both to be 1, NOT A is 0, and (NOT A) AND B is 0 AND 0, which is 0.

Which byte, used with AND as a mask, keeps only the rightmost bit of another byte and clears the rest?

AND passes a bit through only where the mask has a 1. With a single 1 in the rightmost column, every other bit is forced to 0.