AQA 7517, section 4.5.5.1
A decimal digit such as 7 can be stored in a computer in two quite different ways, and you need to know the difference.
Pure binary representation
In pure binary the number is stored as its value, using place values.
- The number 7 is .
- The number 25 is .
This form is used when the number is going to be calculated with, such as added or compared.
Character code representation
In character form, each digit is stored as a character, using its character code (such as ASCII). The digit is treated like any other symbol of text.
The digit characters have these ASCII codes:
| Character | '0' |
'1' |
'2' |
'3' |
'4' |
'5' |
'6' |
'7' |
'8' |
'9' |
|---|---|---|---|---|---|---|---|---|---|---|
| Code (decimal) | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
| Code (binary) | 00110000 | 00110001 | 00110010 | 00110011 | 00110100 | 00110101 | 00110110 | 00110111 | 00111000 | 00111001 |
So the character '7' is stored as (55), while the number 7 is stored as .
Comparing the two
The number 25:
| Representation | Bits used | |
|---|---|---|
| Pure binary | 8 (one byte) | |
| Character form | '2' then '5', |
16 (two bytes) |
| Pure binary | Character code | |
|---|---|---|
| What is stored | The value | A symbol for each digit |
| Space | Compact | One byte (or more) per digit |
| Arithmetic | Can be used directly | Must be converted first |
| Typical use | Calculations | Display, input and output, and data that is not used in calculations |
When is each used?
Use character form for numbers that are not used in calculations, such as:
- phone numbers (and the leading 0 must be kept);
- postcodes and bank account numbers;
- ID numbers.
Use binary for values that you will calculate with, such as ages, prices and quantities.
Input from a keyboard arrives as characters, so a program must convert to a number before doing arithmetic:
text = "25" # characters '2' and '5'
number = int(text) # converted to the number 25
print(number + 1) # 26
print(ord("7")) # 55: the character code of '7'
print(ord("7") - ord("0")) # 7: the digit's value
The subtraction ord(char) - 48 turns a digit character into its numeric value, because the codes of '0' to '9' run consecutively from 48.
Worked example
Task: show how the number 306 would be stored as characters in ASCII, and compare with pure binary.
- Characters:
'3'is 51,'0'is 48 and'6'is 54. In binary: (3 bytes, 24 bits). - Pure binary: , which needs 9 bits (so 2 bytes, as ).
Character form takes up more space, and the bits are completely different: they represent the symbols 3, 0 and 6, not the value three hundred and six.
Exam tip: be precise about what is stored. The character '5' has the code 53 (), which is not the number 5 (). Mention that numbers read as text have to be converted before arithmetic.
Check yourself
What is the ASCII code of the character '0', and how is it different from the number 0?
The character '0' has code 48 (), but the number 0 in pure binary is .
Why is a phone number usually stored as characters, not as a binary number?
You never calculate with it, and a leading 0 must be kept, which a binary number would lose.
How can the numeric value of a digit character be found from its ASCII code?
Subtract 48 (the code for '0'), since the digit codes are consecutive.