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:
- Describe how place value works in binary and identify the most and least significant bits
- Convert whole numbers between binary and denary (base 10) up to 8 bits
- Add two binary numbers by hand, including carries, and recognise overflow
- Explain how a voltage on a wire is turned into a bit, and why computers use two levels rather than ten
- Describe the Boolean values true and false and apply the NOT, AND and OR operations to single bits and whole bytes
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.
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.
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.
- Set only the rightmost bit to 1. What value do you get?
- Now set every bit to 1. This is the largest number 8 bits can hold.
- Set the bits to make 100. Which columns did you need?
- Click 16-bit, then All 1s. What is the largest number 16 bits can hold?
- 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?
How many different values can be represented with 4 bits?
What is the largest denary number that can be stored in 16 bits?
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.
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:
| Column | Fits into what is left? | Bit | Left over |
|---|---|---|---|
| 128 | No (77) | 0 | 77 |
| 64 | Yes | 1 | 13 |
| 32 | No | 0 | 13 |
| 16 | No | 0 | 13 |
| 8 | Yes | 1 | 5 |
| 4 | Yes | 1 | 1 |
| 2 | No | 0 | 1 |
| 1 | Yes | 1 | 0 |
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?
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.
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?
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.
- 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.
- Click Send a new byte a few times and watch how the signal shape follows the bits.
- 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?
What is the main reason computers use two voltage levels instead of ten?
Using the diagram, roughly how much noise could the signal take before bits started to be misread? Explain why it is about that value.
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.
| A | B | NOT A | A AND B | A OR B |
|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 1 |
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?
A = 1 and B = 0. Which expression is true (equal to 1)?
Which byte, used with AND as a mask, keeps only the rightmost bit of another byte and clears the rest?