KEMBAR78
G3 Computing Textbook Chapter 02 | PDF | Decimal | Arithmetic
0% found this document useful (0 votes)
40 views33 pages

G3 Computing Textbook Chapter 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views33 pages

G3 Computing Textbook Chapter 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CHAPTER

02 Data Representation

19
20
21
2.1 Introduction to Data Representation

As you have learned, in a computer, data is stored as bits. To work with various types of data, such as
numbers, text, images, and audio, this information must first be converted into bits that the computer can
understand. Different formats may be used for conversion depending on specific requirements and the
type of data being represented.

In this chapter, we will learn about how two types of data, numbers and text, are typically represented as
bits on a computer using number systems and extended ASCII respectively.

2.2 Understanding Number Systems and Conversion Techniques

LEARNING OUTCOMES

1.2.1 Represent positive whole numbers in binary form.

1.2.2 Convert positive whole numbers from one number system to another - binary, denary
and hexadecimal; and describe the technique used.

2.2.1 The Denary Number System

0 1 2 3 4 5 6 7 8 9
Table 2.1 Digits of the denary number system

ER MS
KEY T
The number system that we are most familiar with and
typically use is called the denary number system. It
is also known as the decimal number system and is
Denary number system
made up of 10 distinct digits.
(or decimal number system)
A number system that is made up of 10
unique digits.

22
As the denary number system has 10 digits, it can
also be called a base-10 system. This is how we
count using the denary number system:
Place value=103 Place value=101
0 1 thousands 8 tens
1
2

9 When the biggest digit, 9, is reached, 1 2 8 9


we add a digit 1 on the left and reset
10 the original digit to 0.
11
Place value=102 Place value=100
12
2 hundreds 9 ones
When the digit on the right reaches
19 9 again, we increase the digit on the
Figure 2.2 Place values of a denary number
20 left by 1 and reset the digit on the
21 right to 0.
The position of a digit in a denary number
determines its place value, which is
N
99 When both digits reach 9, we add a represented by 10 . The place value starts from
0
100 digit 1 on the left and reset the two 10 for the rightmost digit, with the power (N)
101 digits on the right to 0. increasing by 1 for each subsequent digit on
the left, as shown in Figure 2.2. This means
that the place value of each digit in the denary
number system is 10 times the place value of
Figure 2.1 Counting in the denary number system the digit on its immediate right.

hundred ten thousands hundreds tens ones


thousands thousands

Place value 105 104 103 102 101 100

Denary digit
1 2 8 9

Table 2.2 1289 in the denary number system

Figure 2.3 shows how the value of 1289 can be calculated as the sum of each digit multiplied by its place value:

Note that 1289 does not have any digits to the left of (1 × 103) + (2 × 102) + (8 × 101) + (9 × 100)
the digit in the thousands place. The missing digits = 1289
in the ten thousands place and higher are thus
implied to be 0. These implied digits are usually Figure 2.3 Calculating the value of a denary number
left out for denary numbers, but may sometimes
be included, as leading zeros, when an exact
number of digits is needed. For instance, 1289 may
ER MS
be written as 001289 to use exactly six digits in KEY T
the denary number system. This may be useful to
make numbers printed in a column appear neater Leading zero
or easier to read. The zero digit (0) that is to the left of the
first non-zero digit in a number.
Figure 2.4 shows that 001289 still has a value of
1289 even with the leading zeros included:

(0 × 105) + (0 × 104) + (1 × 103) + (2 × 102) + (8 × 101) + (9 × 100) = 1289

Figure 2.4 Calculating the value of a denary number with leading zeros

23
Denary numbers with many digits can also be difficult to read. To make interpreting such numbers easier,
denary numbers larger than 999 are often shown with a separator (either a space or comma) after every
group of three denary digits, starting from the right. For instance, the number 1234567890 can also be
written as 1,234,567,890 or 1 234 567 890.

2.2.2 The Binary Number System

0 1
ER MS
Table 2.3 Digits of the binary number system
KEY T
As computers store data using bits or binary digits, Binary number system
they cannot use the denary number system with A number system that is made up of two
its 10 distinct digits directly. Instead, they use the unique digits.
binary number system that is made up of two
distinct digits, 0 and 1, to store numbers.

As the binary number system has two digits, it can also be called a base-2
system. This is how we count using the binary number system:

0 When the biggest digit, 1, is reached, we add a digit 1 on


1 the left and reset the original digit to 0.
10
11
When both digits reach 1, we add a digit 1 on the left and
100 reset the two digits on the right to 0.
101
110
111 When all the digits reach 1, we add a digit 1 on the left
1000 and reset the digits on right to 0.
1001
1010
1011

Figure 2.5 Counting in the binary number system

The position of a digit in a binary number determines its place value, which is represented by 2N. The place
value starts from 20 for the rightmost digit, with the power (N) increasing by 1 for each subsequent digit on
the left, as shown in Figure 2.6. This means that the place value of each digit in the binary number system
is two times the place value of the digit on its immediate right.

Place value=23 Place value=21 To distinguish binary numbers from denary numbers,
the binary numbers are appended with the number 2
as a subscript, like this: 11012. Alternatively, the binary

1 0 1 1 number may be prefixed with the characters “0b”, like


this: 0b1101.

Just like in denary numbers, leading zeros do not affect


Place value=2 2
Place value=2 0 the value of binary numbers but may be shown when an
exact number of digits is needed. This is common when
Figure 2.6 Place values of a binary number using binary numbers in computer systems as each byte

24
U
DID YO
O W ?
is defined to have exactly eight binary digits.
Leading zeros are often needed so that all K N
eight binary digits of each byte are shown.
Another acceptable way to write binary
For readability, large binary numbers may numbers is to enclose the binary digits in
also be shown with a separator (usually a parentheses, like this: (1101)2. This format is
space) between groups of four digits, starting less ambiguous in situations where normal
from the right. For instance, the binary digits and subscript digits may appear similar
number 100101102 can also be written as in handwriting.
1001 01102.

2.2.2.1 Converting From Binary to Denary

To convert a binary number to its denary equivalent, we use the sum of place
values method, i.e., sum the product of each digit and its place value. Let us use
the binary number 11 10012 as an example.

256 128 64 32 16 8 4 2 1
Place value
28 27 26 25 24 23 22 21 20

Binary digit 1 1 1 0 0 1

Table 2.4 11 10012 in the binary number system

(1 × 25) + (1 × 24) + (1 × 23) + (0 × 22) + (0 × 21) + (1 × 20)


= 32 + 16 + 8 + 0 + 0 + 1 = 57
Figure 2.7 Converting 11 10012 to denary

Hence, the denary equivalent of 11 10012 is 57.

2.2.2.2 Converting From Denary to Binary

There are two methods of converting denary numbers to binary numbers. As an example, let us convert the
denary number 135 to a binary number using each of these methods.

25
2.2.2.2.1 Method 1: Division by 2

In this method, we divide the denary number repeatedly by


2. The remainders of each division are then used to derive the
binary number. The steps to follow are: Using denary number 135 as
an example:
STEP 1 Draw a table with three columns and write down 3 headings – ‘Denary’,
Draw a table with ‘Quotient’ and ‘Remainder’. In the first row of the ‘Denary’ column, write down
three columns – one the denary number that we wish to convert, i.e., 135.
column for denary
numbers, one column
for the quotients and Denary Quotient Remainder
one column for the
remainders.
135

STEP 2 Table 2.5 Steps 1 and 2 in the division by 2 method


Fill in the denary
number in the first row.
Divide the denary number 135 by 2 and write down its quotient and remainder
in the respective columns of the same row. In this case, 135 ÷ 2 gives a quotient
of 67 and a remainder of 1. As the quotient 67 is not zero, copy 67 to the next
row of the ‘Denary’ column, then repeat Step 3 of the algorithm.
STEP 3
Divide the denary Denary Quotient Remainder
number by 2 and fill
in its quotient and
remainder in the 135 67 1
same row.
67

Table 2.6 First iteration of Steps 3 and 4 in the division


by 2 method
STEP 4
If the quotient is 0,
Divide the denary number 67 by 2 and write down its quotient and remainder
proceed to Step 5.
in the respective columns of the same row. In this case, 67 ÷ 2 gives a quotient
Otherwise, copy the
of 33 and a remainder of 1. As the quotient 33 is not zero, copy 33 to the next
quotient to the denary
row of the ‘Denary’ column, then repeat Step 3 of the algorithm.
number column of
the next row and
repeat Step 3.
Denary Quotient Remainder

135 67 1

67 33 1

33

Table 2.7 Second iteration of Steps 3 and 4 in the division by 2 method

26
This sequence is repeated until the last iteration when the denary number
reaches 1. In this case, 1 ÷ 2 gives a quotient of 0 and a remainder of 1. As the
quotient is 0, we proceed to Step 5 of the algorithm.

Denary Quotient Remainder

135 67 1

67 33 1

33 16 1

Direction to read
16 8 0

the digits
8 4 0

4 2 0

2 1 0

1 0 1

Table 2.8 Last iteration of Steps 3 and 4 in the division


by 2 method

STEP 5
The equivalent To obtain the converted binary number, read the digits in the
binary number is the ‘Remainder’ column from the bottom upwards. Hence, the
remainder column binary equivalent of 135 is 1000 01112.
read from the bottom
up.

2.2.2.2.2 Method 2: Sum of Place Values

In this method, we put ‘1’s under the place values, starting


from the left, so that the total adds up to the value that we
want to convert. The steps to follow are: Using denary number 135 as
an example:

In each column, write out the place value of each position in a binary number.
In the last column, write down the denary number we wish to convert, i.e., 135.

STEP 1
Place 256 128 64 32 16 8 4 2 1 Denary
Write out the place value
value of each position in 135
a binary number. 28 27 26 25 24 23 22 21 20

Binary
digit

Table 2.9 Step 1 in the sum of place values method

27
STEP 2
Find the highest place value that is equal to or less than 135. In this case, 27 (or
Find the highest place 128) is the highest place value that is less than 135, so we write ‘1’ under the 27
value that is equal column as shown in Table 2.10.
to or less than the
denary number.

STEP 3 Subtract the place value 128 from 135 and update the result ‘7’ in the ‘Denary’
Fill in the binary digit 1 column, as shown in Table 2.10. As the result is not zero, we repeat Step 2 of
below this place value. the algorithm with the number 7.

Place 256 128 64 32 16 8 4 2 1 Denary


value
135
STEP 4 28 27 26 25 24 23 22 21 20
7
Subtract this place Binary
value from the denary 1
digit
number. If the result
is 0, proceed to Step
5. Otherwise, repeat Table 2.10 First iteration of Steps 2 to 4 in the sum of place values method
Step 2.
Find the highest place value that is equal to or less than 7. In this case, 22
(or 4) is the highest place value that is less than 7, so we write ‘1’ under the 22
column as shown in Table 2.11.

Subtract the place value 4 from 7 and update the result ‘3’ in the ‘Denary’
column, as shown in Table 2.11. As the result is not zero, we repeat Step 2 of
the algorithm with the number 3.

Place 256 128 64 32 16 8 4 2 1 Denary


value
28 27 26 25 24 23 22 21 20 135
7
Binary 3
digit 1 1

Table 2.11 Second iteration of Steps 2 to 4 in the sum of place values method

28
Following the same method, we place ‘1’s under 21 and 20 columns. As the final
number is now 0, we continue with Step 5 of the algorithm.

Place 256 128 64 32 16 8 4 2 1 Denary


value
135
28 27 26 25 24 23 22 21 20 7
3
Binary 1
digit 1 1 1 1 0
STEP 5
Table 2.12 Last iteration of Steps 2 to 4 in the sum of place values method
Fill in the binary
digit 0 below any
remaining place
values.
Fill in ‘0’s under the remaining place value columns, as shown in Table 2.13.

Place 256 128 64 32 16 8 4 2 1


STEP 6 value
The equivalent binary 28 27 26 25 24 23 22 21 20
number is formed
by the row of binary Binary
digit 1 0 0 0 0 1 1 1
digits below the place
values.
Table 2.13 Steps 5 and 6 in the sum of place values method

The binary digit for each place value column is then combined in sequence to form the binary number.
Hence, the binary equivalent of 135 is 1000 01112.

2.2.3 The Hexadecimal Number System

Another number system that is often used in computer


ERMS
systems is the hexadecimal number system. It is made up
of 16 different digits represented by numerals “0” to “9” and
KEY T
the letters “A” to “F”. Hexadecimal number system
A number system that is made up
Here, the letters “A” to “F” are called “digits” as they are of 16 unique digits.
used to represent the 11th (value 10) to 16th (value 15) digits
of the hexadecimal number system as shown in Table 2.14.

0 1 2 3 4 5 6 7 8 9 A B C D E F
Table 2.14 Digits of the hexadecimal number system

29
The hexadecimal number system is also known as the base-16 system.

To count using the hexadecimal number system, we use the following method:

0
1
2

F When the biggest digit, F, is reached, we add a digit 1


10 on the left and reset the original digit to 0.
11
12

When the digit on the right reaches F again, we


1F increase the digit on the left by 1 and reset the digit
20 on the right to 0.
21

FF When both digits reach F, we add a digit 1 on the left


100 and reset the two digits on the right to 0.
101

Figure 2.8 Counting in the hexadecimal number system

This method of counting illustrates how digits “A” to “F” are equivalent to the denary values of 10 to 15
respectively, as shown in Table 2.15.

Hexadecimal digit 0 1 2 3 4 5 6 7 8 9 A B C D E F
Denary value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Table 2.15 Denary equivalents of the hexadecimal digits

Place value=163 Place value=161


The position of a digit in a hexadecimal number
determines its place value, which is represented by 16N.
The place value starts from 160 for the rightmost digit,
with the power (N) increasing by 1 for each subsequent 1 C 6 1
digit on the left, as shown in Figure 2.9. This means
that the place value of each digit in the hexadecimal
Place value=162 Place value=160
number system is 16 times the place value of the digit
on its immediate right. Figure 2.9 Place values of a hexadecimal number

To distinguish hexadecimal numbers from binary and denary numbers, they are often written with a
subscript ‘16’, e.g., 1C6A16. Alternatively, hexadecimal numbers may also be prefixed with the characters
“0x”, e.g., 0x1C6A.

U
DID YO
?
K NOW
As with binary numbers, another acceptable way of writing hexadecimal
numbers is to enclose the hexadecimal digits in parentheses, like this:
(1C6A)16.

30
Just like denary and binary numbers,
U
leading zeros do not affect the value of
DID YO
KNOW?
hexadecimal numbers but may be shown
when an exact number of digits is needed.
This is common when using hexadecimal
numbers in computer systems as each byte
needs at most two hexadecimal digits to An easy way to convert values between different
be shown in full. Leading zeros are often number systems is to use the Calculator app on your
needed so that each byte can be expressed computer. In Windows operating systems, you can
using exactly two hexadecimal digits. find this app under the Programs/Apps folder of the
Start Menu. In Mac operating systems, you can find
For readability, hexadecimal numbers with this app in the Applications folder. For either version,
more than three digits may also be shown set the calculator to Programmer mode by clicking
with a separator (usually a space) between on the “View” menu and selecting “Programmer”
groups of two or four digits, starting from to perform conversions between different number
the right. For instance, the hexadecimal systems.
number 1C6A16 can also be written as
1C 6A16.

2.2.3.1 Converting From Hexadecimal to Binary

To convert a hexadecimal number to its binary equivalent, we map each hexadecimal digit to its four-digit
binary equivalent as shown in Table 2.16.

Hexadecimal digit 0 1 2 3 4 5 6 7

Binary value 0000 0001 0010 0011 0100 0101 0110 0111

Hexadecimal digit 8 9 A B C D E F

Binary value 1000 1001 1010 1011 1100 1101 1110 1111

Table 2.16 Hexadecimal digits and their four-digit binary equivalents

For example, let us convert 3C16 to its binary equivalent.

1 Write down 3C16 as its individual hexadecimal digits,


i.e., ‘3’ and ‘C’.

2 In Table 2.17, we write down the four-digit binary Hexadecimal digit 3 C


equivalents of ‘3’ and ‘C’ found in Table 2.16, i.e. ‘0011’
and ‘1100’ respectively. Binary value 0011 1100

3 Finally, the sets of binary digits in Table 2.17 are combined Table 2.17 Converting 3C16 to binary
sequentially to form the equivalent binary number.
Hence, the binary equivalent of 3C16 is 0011 11002,
which is the same as 11 11002 if the leading zeros are
omitted.

31
As another example, let us convert CF 0716 to its binary equivalent.

As shown in Table 2.18, the binary equivalent


of CF 0716 is 1100 1111 0000 01112. Hexadecimal digit C F 0 7
Binary value 1100 1111 0000 0111

Table 2.18 Converting CF 0716 to binary

2.2.3.2 Converting From Binary to Hexadecimal

To convert a binary number to its hexadecimal equivalent,


we map each set of 4 binary digits to its corresponding For example, let us convert
hexadecimal digit as follows: 10 1000 0110 10112 to
hexadecimal.

Starting from the right, we split the number into four-digit groups, as shown
STEP 1 in Table 2.19.

Split the binary number


into sets of four digits, 10 1000 0110 1011
starting from the right..
Table 2.19 Split the binary number into groups of four digits, starting from the right

STEP 2 Since the final group on the left only contains two digits, we add two leading
If the final set of zeros to its left such that it now has four digits, as shown in Table 2.20.
remaining digits on the
left does not have four
digits, we add leading 0010 1000 0110 1011
zeros to the set such
that it has exactly four Table 2.20 Add leading zeros to the final group on the
binary digits. left if it does not have four digits

Using Table 2.16, we write down the equivalent hexadecimal digit for each
group of four binary digits as shown in Table 2.21.
STEP 3
Using Table 2.16 as
a reference, map
Binary digits 0010 1000 0110 1011
each four-digit group Hexadecimal digit 2 8 6 B
to its corresponding
hexadecimal digit.
Table 2.21 Converting each group of four binary digits
into a hexadecimal digit

STEP 4
Combine the Finally, the hexadecimal digits in Table 2.21 are combined sequentially to
hexadecimal digits form the equivalent hexadecimal number. Hence, the hexadecimal equivalent
sequentially to of 10 1000 0110 10112 is 28 6B16.
form the equivalent
hexadecimal number.

32
2.2.3.3 Converting From Hexadecimal to Denary

To convert a hexadecimal number to its denary equivalent,


we use the sum of place values method as follows: Let us convert C7F16 to denary.

From Table 2.15, the denary values of ‘C’, ‘7’ and ‘F’ are 12, 7 and 15
respectively.

STEP 1 65536 4096 256 16 1


Convert each Place value
hexadecimal digit to 164 163 162 161 160
its equivalent denary
value. Hexadecimal digit C 7 F

Denary equivalent 12 7 15

Table 2.22 C7F16 in the hexadecimal number system

Multiply the denary equivalent of each digit with its place value as
STEP 2 shown in Figure 2.10 and sum up the products.
Multiply each
denary value by the (12 × 162) + (7 × 161) + (15 × 160) = 3072 + 112 + 15 = 3199
respective 16N place
Figure 2.10 Converting C7F16 to denary
values.

STEP 3
Sum the results of Hence, the denary equivalent of C7F16 is 3199.
each place value.

2.2.3.4 Converting From Denary to Hexadecimal

There are two methods of converting denary numbers to hexadecimal numbers. As an example, let us
convert the denary number 1899 using each of these methods.

2.2.3.4.1 Method 1: Division by 16

In this method, we divide the denary number repeatedly by 16. This is similar to the ‘division by 2’
method in section 2.2.2.2.1 used to convert a denary number to binary. The remainders of the divisions
are then used to derive the hexadecimal number.

33
The general algorithm is as follows:
Using 1899 as an example:

STEP 1
Draw a table with three columns and write down 3 headings – ‘Denary’,
Draw a table with
‘Quotient’ and ‘Remainder’. In the first row of the ‘Denary’ column, write down
three columns – one
the denary number that we wish to convert, i.e, 1899.
column for denary
numbers, one column
for the quotients and
one column for the Denary Quotient Remainder
remainders.
1899

Table 2.23 Steps 1 and 2 in the division by 16 method

STEP 2
Fill in the denary
number in the first row. Divide the denary number 1899 by 16 and write down its quotient and
remainder in the respective columns of the same row. In this case, 1899 ÷ 16
gives a quotient of 118 and a remainder of 11. As the quotient 118 is not zero,
copy 118 to the next row of the ‘Denary’ column, then repeat Step 3 of the
algorithm.
STEP 3
Divide the denary
number by 16 and Denary Quotient Remainder
fill in its quotient
and remainder in the 1899 118 11
same row.

118

Table 2.24 First iteration of Steps 3 and 4 in the division by 16 method


STEP 4
If the quotient is 0,
proceed to Step 5.
Divide the denary number 118 by 16 and write down its quotient and remainder
Otherwise, copy the
in the respective columns of the same row. In this case, 118 ÷ 16 gives a quotient
quotient to the denary
of 7 and a remainder of 6. As the quotient 7 is not zero, copy 7 to the next row of
number column of
the ‘Denary’ column, then repeat Step 3 of the algorithm.
the next row and
repeat Step 3.
Denary Quotient Remainder

1899 118 11

118 7 6

Table 2.25 Second iteration of Steps 3 and 4 in the


division by 16 method

34
Divide the denary number 118 by 16 and write down its quotient and remainder
in the respective columns of the same row. In this case, 118 ÷ 16 gives a quotient
of 7 and a remainder of 6. As the quotient 7 is not zero, copy 7 to the next row of
the ‘Denary’ column, then repeat Step 3 of the algorithm.

Denary Quotient Remainder

1899 118 11

118 7 6

7 0 7

Table 2.26 Last iteration of Steps 3 and 4 in the division by 16 method

Convert the numbers in the remainder column to hexadecimal digits.

Denary Quotient Remainder


STEP 5
1899 118 11 = B16

Direction to read
Convert the
numbers in the
remainder column to 118 7 6= 616

the digits
hexadecimal digits.
7 0 7= 716

Table 2.27 Steps 5 and 6 in the division by 16 method

STEP 6
The converted To obtain the converted hexadecimal number, read the hexadecimal
hexadecimal number digits in the ‘remainder’ column upwards from the bottom. Hence,
is the remainder the hexadecimal equivalent of 1899 is 76B16.
column read from the
bottom up.

2.2.3.4.2 Method 2: Sum of Place Values

In this method, we put hexadecimal digits under the place values, starting from the left, so that the
total adds up to the value that we want to convert. This method is similar to the sum of place values
method used in section 2.2.2.2.2 to convert from a denary number to binary. However, it is significantly
more complex as there are more digits to work with in the hexadecimal number system compared to
the binary number system.

35
The general algorithm is as follows:
Using the denary number 1899
as an example:
STEP 1 In each column, write out the place value of each position in a hexadecimal
number. In the last column, write down the denary number we wish to convert,
Write out the place
i.e., 1899.
value of each position
in a hexadecimal
number. 65536 4096 256 16 1 Denary
Place value
164 163 162 161 160 1899

Hexadecimal
STEP 2 digit
Find the highest place
value that is equal to Table 2.28 Step 1 in the sum of place values method
or less than the denary
Find the highest place value that is equal to or less than the denary number
number.
1899. In this case, 162 (or 256) is the highest place value that is less than 1899.

STEP 3
Fill in the largest Under the column of 162, we fill in the largest hexadecimal digit such that its
hexadecimal digit product with 162 is equal to or less than 1899. Since 716 × 162 (= 1792) is less
below this place than 1899 and 816 × 162 (= 2048) is greater than 1899, the largest hexadecimal
value whose product digit needed here is 716, as shown in Table 2.29.
with this place value
is equal to or less
than the denary
number.

Subtract the product 1792 from 1899 and update the result ‘107’ in the denary
column, as shown in Table 2.29. As the result is not zero, we repeat Step 2 of
the algorithm with the number 107.
STEP 4
Subtract this product
from the denary 65536 4096 256 16 1 Denary
number. If the result Place value
is 0, proceed to Step 164 163 162 161 160 1899
5. Otherwise, repeat 107
Step 2. Hexadecimal
7
digit

Table 2.29 First iteration of Steps 2 to 4 in the sum of place values method

Find the highest place value that is equal to or less than the denary number
107. In this case, 161 (or 16) is the highest place value that is less than 107.
Hence, under the column of 161, we fill in the largest hexadecimal digit such
that its product with 161 is equal to or smaller than 107. Since 616 × 161 (= 96) is
less than 107 and 716 × 161 (= 112) is greater than 107, the largest hexadecimal
digit needed here is 616, as shown in Table 2.30.

Subtract the product 96 from 107 and update the result ‘11’ in the ‘Denary’
column, as shown in Table 2.30. As the result is not zero, we repeat Step 2 of the
algorithm with the number 11.

36
65536 4096 256 16 1 Denary
Place value
164 163 162 161 160 1899
107
Hexadecimal 11
7 6
digit

Table 2.30 Second iteration of Steps 2 to 4 in the sum of place values method

Find the highest place value that is equal to or less than the denary number
11. In this case, 160 (or 1) is the highest place value that is less than 11. Hence,
under the column of 160, we fill in the largest hexadecimal digit whose product
with 160 is equal to or smaller than 11. Since B16 × 160 (11 × 1 = 11) is equal to 11,
the hexadecimal digit needed here is B16, as shown in Table 2.31.

Subtract the product 11 from 11, which gives us 0, as shown in Table 2.31. As
the result is 0, we can proceed to Step 5 of the algorithm.

65536 4096 256 16 1 Denary


Place value
164 163 162 161 160 1899
107
Hexadecimal 11
7 6 B
digit 0

Table 2.31 Last iteration of Steps 2 to 4 in the sum of place values method

STEP 5
Fill in the
Fill in 0s under the remaining place values. In this case, all the place values
hexadecimal digit 0
after the first not zero hexadecimal digit are already filled.
below any remaining
place values.

STEP 6
The equivalent
hexadecimal number Hence, the hexadecimal equivalent of 1899 is 76B16.
is formed by the row
of hexadecimal digits
below the place
values.

37
QUICK 2
C K 2.
CHE
1. Convert the following denary numbers to binary using both the division by 2 and sum of place values methods:
a) 54
b) 89
c) 100

2. Convert the denary numbers 7, 8, 9 and 10 to their binary equivalents.

3. Convert the following binary numbers to denary:


a) 100112
b) 110 00002.
c) 1000 01102

4. The largest number that can be represented by N bits is obtained when all the N bits are set to 1. The following
is a table of the largest numbers that can be represented by N bits, where N = 1 to 5, expressed in both binary
and denary:

N Binary Denary
1 12 1=2-1
2 112 3=4-1
3 1112 7=8-1
4 11112 15 = 16 - 1
5 111112 31 = 32 - 1

a) Calculate the largest denary number that can be represented using:


i. 6 bits
ii. 8 bits
iii. 16 bits

b) By observing a pattern or through other means , express the largest number that can be represented using
N bits in terms of N

5. At least 16 bits are required to represent the sum of two 8-bit numbers. True or false?

6. Write down the hexadecimal and binary equivalents of the denary values from 32 to 47.

7. Convert the following hexadecimal numbers to binary:


a) 4916
b) AB616
c) DA4716

8. Convert the following hexadecimal numbers to denary:


a) 9D16
b) BB616
c) AD9016

9. Convert the following denary numbers to hexadecimal:


a) 1000
b) 3789
c) 4220

38
2.3 Representing Negative Numbers

LEARNING OUTCOMES

1.2.3 Use two’s complement for a fixed number of bits to represent both positive and negative
whole numbers in binary.

So far, we have only used bits and the binary number system to represent zero and positive integers.
However, computers often use bits to represent negative integers as well.

U
DID YO
?
K NOW
There are many ways to represent negative integers in binary form on a
computer. One simple way is to reserve the first bit (the leftmost bit) of a binary
number to represent the positive sign (0 as ‘+’) or negative sign (1 as ‘-‘). The
remaining bits then represent the magnitude (size) of the positive or negative
integer. For instance, with 3 bits, 001 would represent integer ‘1’ and 101 would represent the integer ‘-1’.
This is called the sign-magnitude representation.

Unfortunately, this representation has some disadvantages. For instance, in this representation, there are
two ways to represent zero (e.g., with 3 bits, 000 and 100 both represent 0). The steps for performing
binary arithmetic operations also differ depending on whether sign-magnitude representation is used or
not. These disadvantages make the sign-magnitude representation more difficult to work with in practice
and so it is not used by most modern computers.

In most computers, negative integers are represented using a system where a fixed number of bits are used
to represent each integer. For simplicity, suppose we have a computer where each integer is represented
by 3 bits. Figure 2.11 shows a number line of all the positive integers that can be represented using 3 bits,
starting from 0 to 7 (= 23 – 1). The numbers are shown as bits above the line and their corresponding integer
values are below the line:

Figure 2.11 Number line of positive 3-bit numbers

To represent negative integers, we shift the numbers to the right such that zero is near the centre, as shown
in Figure 2.12. We then transfer (or wrap) the higher order bits that begins with 1 to the left end of the
number line, keeping them in the same sequence. However, instead of representing positive integers (4, 5,
6, 7), these bits now represent negative integers (-4, -3, -2, -1).

Figure 2.12 Shifted number line to represent negative numbers

39
Keeping the same relative order allows the processor to perform the same binary addition and subtraction
operations for both positive and negative integers.

Denary 3-bit two’s complement


Addition 2 0 1 0
+ 1 + 0 0 1
3 0 1 1

Subtraction 3 0 1 1
- 1 - 0 0 1
2 0 1 0

Figure 2.13 Addition and subtraction of positive integers in denary and 3-bit two’s complement

This is especially true when transitioning between ‘-1’ (represented by 111) and ‘0’ (represented by 000).
This is because given a fixed number of bits, binary operations that result in a binary values which that
exceeds the limits of the representable range will simply “wrap around” such that the final bit representation
stays within the range. This behaviour is called integer overflow.

• For instance, adding 1 to 1112 (represented by 111) should produce 10002, but this result cannot
be represented using 3 bits, so it is “wrapped around” to produce 000. Note that this is consistent
with the interpretation where 111 represents ‘-1’ and 000 represents ‘0’.

Denary 3-bit two’s complement


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

-1 1 1 1
+ -3 + 1 0 1
-4 1 1 0 0

Figure 2.14 Addition of negative integers in denary and 3-bit two’s complement

As a result of the shift shown in Figure 2.12, the bit representation of numbers that are the negative of each
other (e.g., 3 and -3) can be converted from one to the other by calculating the two’s complement.

The two’s complement is defined as 2N minus the number (where N is the number of bits).

• For instance, 3 is represented by 011 using 3 bits. The two’s complement is 23 – 0112 = 8 – 3 = 5 =
1012. This corresponds to 101, which represents -3.
• Conversely, -3 is represented by 101 using 3 bits. The two’s complement is 23 – 1012 = 8 – 5 = 3 =
0112. This corresponds to 011, which represents 3.

Figure 2.15 Numbers that are negative of each other are related by two’s complement

40
U
Instead of performing denary arithmetic operations to calculate
DID YO
KNOW?
2N minus the number, a quicker method (both for humans and
the computers) is to perform simple binary operations as follows:

1) Flip all N bits (i.e., replace 0 with 1 and 1 with 0).


2) Add 1 to the binary number. A “complement” is what you
3) If the number of bits exceeds N, drop the highest combine with the original to
(leftmost) bit. This step is known as an overflow. make something “complete”.
For instance, the “complement”
For instance, we represent ‘-54’ with 8 bits by using this method of a mathematical set is a set of
with the binary form of ‘54’: all the elements that are not in
the original set. Similarly, a ten’s
1) Convert 54 to its binary form (see the two methods complement is the number that
under section 2.2.2.2): 54 = 11 01102 must be added to make 10, so
2) By adding leading zeros, 54 is represented by 0011 the ten’s complement of 3 is 7
0110 using 8 bits. and the ten’s complement of 6
3) Flip all 8 bits: 1100 1001 is 4.
4) Add 1 to the binary: 1100 1010
Hence, -54 is represented by 1100 1010 using 8 bits. In the same way, the two’s
complement of a number for N
By convention, the name “two’s complement” also refers to the bits is the number that must be
operation of calculating the two’s complement of a number. The added to make 2N.
overall system of representing negative numbers as described
above is also called the “two’s complement representation”.

The two’s complement representation has some useful


ER MS
and important features: KEY T
• The first bit always represents the number’s Integer overflow
sign (0 if positive, 1 if negative). Result when an operation produces an
• Zero is uniquely represented by having all N integer that exceeds the limits of the
bits set to 0. representable range
• The range of numbers that can be represented
with N bits is strictly defined as -2N-1 to 2N-1 – 1. Two’s complement
System used by computers to represent
both positive and negative numbers

QUICK
E CK 2 .3
CH
1. Represent the following numbers using 4-bit two’s complement:
a) 5
b) -5
c) 7
d) -7
e) -8

2. Both 8 and -8 can be represented using 4-bit two’s complement. True or False?

3. The following sets of 8 bits use two’s complement to represent negative numbers. Write down the number
being represented by each set of 8 bits.
a) 1100 1010
b) 0101 0011
c) 1111 1111
d) 1000 0000

41
2.4 Representing Text

LEARNING OUTCOMES

1.2.4 Use the example of an 8-bit extended ASCII encoding for English text to explain how
information can be represented as bits for storage or processing by a computer.

For text data, each symbol, letter, digit or space is called


ER MS
a character. Each character is usually represented by
a binary or hexadecimal number when transmitted
KEY T
or stored digitally in a computer system. Character Character (text)
encoding defines how these numbers (or codes) are A unit of textual data, usually a symbol,
assigned to each unique character. Using character letter, digit or space
encoding, text is represented on a computer by
mapping each character to numbers and storing them Character encoding
as sequences of bits and bytes. The assignment of numbers to characters

A common character encoding method for English


characters, including upper-case and lower-case
letters, is based on the American Standard Code for
Information Interchange (ASCII). ASCII has been widely
used since the 1960s. The original ASCII standard used 7
bits to represent each character, so only 27 = 128 distinct
characters could be represented.

U
DID YO
?
KNOW
ASCII was developed in an era where teleprinters or teletypewriters were commonly used for
communication.

Prior to the use of magnetic storage media, these devices encoded information digitally on paper
tapes by punching holes in them. A punched hole represented 1 while a position with no hole
represented 0. The image below shows how 8 bits in a row are used to represent a character, with
7 bits for ASCII encoding and 1 additional bit on the left, known as the parity bit (see Chapter 10).

Figure 2.16 Teletype Model 33 with paper tape Figure 2.17 Segment of a punched paper tape
reader and punch on its left side showing sequences of 8 bits

42
U
Subsequent Extended ASCII standards then DID YO
?
K NOW
used 1 byte (8 bits) for each character, so they
could represent an additional 128 characters
for a total of 28 = 256 distinct characters.
The name “Extended ASCII” can refer to several
With Extended ASCII, the 256 characters are standards. For languages like English that
represented by numbers from 0000 00002 are based on the Latin alphabet, “Extended
to 1111 11112, which is the same as 0016 to ASCII” often refers to the “Latin 1” standard
FF16 or 0 to 255. Hexadecimal digits are often defined by the International Organisation for
used to express Extended ASCII codes as two Standardisation (ISO).
hexadecimal digits correspond exactly to 1 byte
or 1 character. This is more compact than using The first 32 ASCII codes (0 to 31) represent non-
three denary digits or eight binary digits. printing characters that are not written symbols
or the space character. These codes are used to
Table 2.32 shows how the ASCII codes from 32 to perform special functions on devices (such as
127 may be expressed using the denary (base- the Teleprinter in Figure 2.16) or to represent
10), binary (base-2) and hexadecimal (base-16) special input such as typing the Backspace and
number systems along with the corresponding Esc keys on the keyboard. The last ASCII code
characters being represented. 127 is also used to represent the special input of
typing the Delete key on the keyboard.

Base-10 Base-2 Base-16 Character Base-10 Base-2 Base-16 Character

32 010 0000 20 (space) 50 011 0010 32 2

33 010 0001 21 ! 51 011 0011 33 3

34 010 0010 22 ” 52 011 0100 34 4

35 010 0011 23 # 53 011 0101 35 5

36 010 0100 24 $ 54 011 0110 36 6

37 010 0101 25 % 55 011 0111 37 7

38 010 0110 26 & 56 011 1000 38 8

39 010 0111 27 ’ 57 011 1001 39 9

40 010 1000 28 ( 58 011 1010 3A :

41 010 1001 29 ) 59 011 1011 3B ;

42 010 1010 2A * 60 011 1100 3C <

43 010 1011 2B + 61 011 1101 3D =

44 010 1100 2C , 62 011 1110 3E >

45 010 1101 2D - 63 011 1111 3F ?

46 010 1110 2E . 64 100 0000 40 @

47 010 1111 2F / 65 100 0001 41 A

48 011 0000 30 0 66 100 0010 42 B

49 011 0001 31 1 67 100 0011 43 C

43
Base-10 Base-2 Base-16 Character Base-10 Base-2 Base-16 Character

68 100 0100 44 D 98 110 0010 62 b

69 100 0101 45 E 99 110 0011 63 c

70 100 0110 46 F 100 110 0100 64 d

71 100 0111 47 G 101 110 0101 65 e

72 100 1000 48 H 102 110 0110 66 f

73 100 1001 49 I 103 110 0111 67 g

74 100 1010 4A J 104 110 1000 68 h

75 100 1011 4B K 105 110 1001 69 i

76 100 1100 4C L 106 110 1010 6A j

77 100 1101 4D M 107 110 1011 6B k

78 100 1110 4E N 108 110 1100 6C l

79 100 1111 4F O 109 110 1101 6D m

80 101 0000 50 P 110 110 1110 6E n

81 101 0001 51 Q 111 110 1111 6F o

82 101 0010 52 R 112 111 0000 70 p

83 101 0011 53 S 113 111 0001 71 q

84 101 0100 54 T 114 111 0010 72 r

85 101 0101 55 U 115 111 0011 73 s

86 101 0110 56 V 116 111 0100 74 t

87 101 0111 57 W 117 111 0101 75 u

88 101 1000 58 X 118 111 0110 76 v

89 101 1001 59 Y 119 111 0111 77 w

90 101 1010 5A Z 120 111 1000 78 x

91 101 1011 5B [ 121 111 1001 79 y

92 101 1100 5C \ 122 111 1010 7A z

93 101 1101 5D ] 123 111 1011 7B {

94 101 1110 5E ^ 124 111 1100 7C |

95 101 1111 5F _ 125 111 1101 7D }

96 110 0000 60 ` 126 111 1110 7E ~

97 110 0001 61 a 127 111 1111 7F (delete)

Table 2.32 ASCII codes converted to binary and hexadecimal numbers and their corresponding characters

44
U
DID YO
N O W ? Besides ASCII, an expanded standard called Unicode is used to represent
K characters in languages other than English such as Tamil and Mandarin. Like
ASCII, the Unicode standard uses numbers to represent characters. However,
while Extended ASCII codes are defined to be exactly 1 byte long, Unicode is not
as restrictive. In Unicode, the number of bits used to represent each character can vary from 8 to 32 bits
depending on the encoding scheme used. This flexibility means that Unicode can be used to represent over
a million unique characters from many written languages around the world, which makes it the preferred
choice over the ASCII standard.

QUICK 4
C K 2.
CHE
1. Use an ASCII table (such as Table 2.32) to determine the ASCII code for the following characters:
a) @
b) z
c) 1
d) (space)
e) ?

2. Decode the following sequence of bytes using an ASCII table. Each byte represents a character and is
encoded as 2 hexadecimal digits:

51 75 69 65 74 2C 20 70 6C 65 61 73 65 21

W
REVIE
E STI ON
Q U
1. Why is the hexadecimal number system preferred over the binary number system in representing large
numbers?

2. Convert the following hexadecimal numbers to binary:


a) A16
b) FF16
c) 10B16

3. Convert the following binary numbers to hexadecimal:


a) 11012
b) 10012
c) 1012
d) 1110 11012
e) 101 01012
f) 1 0110 1100 11012

45
W
REVIE N
S TI O
QUE
4. a) Convert the denary number 108 to an 8-bit binary number.
b) Convert the denary number 108 to hexadecimal.

5. a) Use two’s complement to represent -1 for:


i. 2 bits
ii. 4 bits
iii. 6 bits

b) By observing a pattern, describe how -1 is generally represented using two’s complement for N bits.

6. A lift system in a building uses eight bits to store some important information per lift, as shown in Figure
2.18. The first four bits indicate whether any of the four sensors has picked up a fault (denoted by the value
1) in any of the subsystems. The last four bits indicate the lift number. There are 15 lifts in the building.

Place values 8 4 1 2

Lift
number
Display Lighting Motor Ventilator
sensor sensor sensor sensor

Figure 2.18 The eight bit register used in a lift system

For example, the bits shown in Figure 2.19 indicates that there are faults in the lighting and motor
subsystems for lift number 3.

0 1 1 0 0 0 1 1
Figure 2.19 Bits indicating that lift number 3 has faults in the
lighting and motor subsystems

a) What is indicated by the bits shown in Figure 2.20?

1 0 1 0 0 1 1 1
Figure 2.20 Bits for lift system

b) What is indicated by the bits shown in Figure 2.21?

0 0 1 1 1 0 1 1
Figure 2.21 Bits for lift system

c) What would be the bits if there were faults in the display and lighting subsystems for lift number 14?
Give your answer as a binary number.

46
W
REVIE N
S TI O
QUE
7. In a factory, there are 127 electrical points that provide power to various machinery. Eight bits are used
to represent the state of each electrical point. The first bit indicates the operational state of the electrical
point: a value of 1 means that it is operational and a value of 0 means that it is faulty. The next seven bits
indicate the electrical point number.

For example, the eight bits shown in Figure 2.22 indicates that electrical point number 41 is operational.

Place values 64 32 16 8 4 2 1

1 0 1 0 1 0 0 1
Figure 2.22 Bits indicating that electrical point number 41 is operational

a) What is indicated by the bits shown in Figure 2.23?

Place values 64 32 16 8 4 2 1

1 0 1 1 1 0 0 0
Figure 2.23 Bits for electrical point

b) What would be the bits if electrical point number 36 was faulty? Give your answer as an eight-bit binary
number.

c) Convert the number indicated by the bits shown in Figure 2.24 to hexadecimal.

1 0 0 1 1 1 0 0
Figure 2.24 Bits for electrical point

47
ANSWER

Pg. 38-Quick Check 2.2


1. Working tables are expected to be shown as part of the answer.
a) 11 01102
Using the division by 2 method:
Denary Quotient Denary
54 27 0
27 13 1
Direction to
13 6 1 read the digits
6 3 0
3 1 1
1 0 1

Using the sum of place values method:


256 128 64 32 16 8 4 2 1 Denary
Place value
2 8
27
26
25
24
23
22
21
20 54
22
6
Binary digit 1 1 0 1 1 0
2
0

b) 101 10012
Using the division by 2 method:
Denary Quotient Denary
89 44 1
44 22 0
22 11 0
Direction to
11 5 1
read the digits
5 2 1
2 1 0
1 0 1

Using the sum of place values method:


256 128 64 32 16 8 4 2 1 Denary
Place value
28 27 26 25 24 23 22 21 20 89
25
9
Binary digit 1 0 1 1 0 0 1
1
0

c) 110 01002
Using the division by 2 method:
Denary Quotient Denary
100 50 0
50 25 0
25 12 1
12 6 0 Direction to
6 3 0 read the digits
3 1 1
1 0 1

48
ANSWER

Using the sum of place values method:


256 128 64 32 16 8 4 2 1 Denary
Place value
28 27 26 25 24 23 22 21 20 100
36
4
Binary digit 1 1 0 0 1 0 0
0

2. 7 = 1112 (leading zeros are not necessary here)


8 = 10002
9 = 10012
10 = 10102

3. a) 19
b) 96
c) 134

4. a) The largest number is when all the bits are 1.


i. 1111112 = 63
ii. 1111 11112 = 255
iii. 1111 1111 1111 11112 = 65,535

b) The largest number that can be represented using N bits is 2N – 1. (As observed in the table.)

Alternatively (proving / explanation is not required by the question):


• Using the definition of place values, the largest number can be written as 20+21+22+ ... + 2N-1; or
• Adding 1 to N bits of 1 produces 1 followed by N bits of 0 (e.g., for N = 3, adding 1 to 1112 produces 10002) or
2N. So by working backwards we can obtain 2N – 1.

5. False. The largest 8-bit number is 1111 11112 = 255, so the sum of two 8-bit numbers is at most 255 + 255 = 510,
which is 1 1111 11102 in binary and requires only 9 bits, not 16.

6. All values between 32 to 47 are as shown:


Denary Binary Hexadecimal
32 10 00002 2016
33 10 00012 2116
34 10 00102 2216
35 10 00112 2316
36 10 01002 2416
37 10 01012 2516
38 10 01102 2616
39 10 01112 2716
40 10 10002 2816
41 10 10012 2916
42 10 10102 2A16
43 10 10112 2B16
44 10 11002 2C16
45 10 11012 2D16
46 10 11102 2E16
47 10 11112 2F16

49
ANSWER

7. By mapping each hexadecimal digit:


a) 100 10012
b) 1010 1011 01102
c) 1101 1010 0100 01112

8. Using sum of place values method


a) 157
b) 2998
c) 44432

9. Using division by 16 method or sum of place values method


a) 3E816
b) ECD16
c) 10 7C16

Pg. 41-Quick Check 2.3


1. Leading zeros should be included:
a) 0101
b) 1011
c) 0111
d) 1001
e) 1000

2. False.
(The range of values that can be represented using 4-bit two's complement is from -23 to 23 – 1 which is -8 to 7,
hence -8 can be represented but 8 cannot.)

3. Hint: if the first bit is 1 (negative sign), then perform two’s-complement to find its magnitude (positive
complement)
a) -54
b) 83
c) -1
d) -128

Pg. 45-Quick Check 2.4


1. a) 64
b) 122
c) 49
d) 32
e) 63

2.
Code 51 75 69 65 74 2C 20 70 6C 65 61 73 65 21
Char. Q u i e t , p l e a s e !

Pg. 45-Review Questions

1. When representing large numbers, the hexadecimal form is much shorter than the binary form, making it more
compact for communication.

2. a) 10102
b) 1111 11112
c) 1 0000 10112

50
ANSWER

3. a) D16
b) 916
c) 516
d) ED16
e) 5516
f) 16 CD16

4. a) 0110 11002
b) 6C16

5. a)
i. 11
ii. 1111
iii. 111111

b) When using two's complement for N bits, -1 is represented by setting all N bits to 1.

6. a) There are faults in the display and motor subsystems for lift number 7.
b) There are faults in the motor and ventilator subsystems for lift number 11.
c) 11001110

7. a) Electrical point number 56 is operational.


b) 00100100
c) 9C16

51

You might also like