Mathematics for Computing
Unit 2 – Mathematics for Computing
2.1 Number systems
A number system defines a set of values used to represent quantity. We talk about the
number of people attending class, the number of modules taken per student, and also
use numbers to represent grades achieved by students in tests.
Quantifying values and items in relation to each other is helpful for us to make sense
of our environment. We do this at an early age; figuring out if we have more toys to
play with, more presents and so on.
The study of number systems is not just limited to computers. We apply numbers
every day, and knowing how numbers work will give us an insight into how a
computer manipulates and stores numbers.
The Romans devised a number system, which could represent all the numbers from 1
to 1,000,000 using only seven symbols
• I=1
• V=5
• X = 10
• L = 50
• C = 100
• D = 500
• M = 1000
The number system in most common use today is the Arabic system. It was first
developed by the Hindus and was used as early as the 3rd century BC. The
introduction of the symbol 0, used to indicate the positional value of digits was very
important. We thus became familiar with the concept of groups of units, tens of units,
hundreds of units, thousands of units and so on.
In number systems, it’s often helpful to think of recurring sets , where a set of values
is repeated over and over again.
Considering the decimal number system, it has a set of values, which range from 0 to
9. This basic set is repeated over and over, creating large numbers.
Faculty of Information Technology - University of Moratuwa 58
Mathematics for Computing
0 0
1
2
3
4
5
6 Basic set 0-9
Vertical Repetition
7 Horizontal Repetition
8
9
1 10
11
12
13
14
15
16
17
18
19
20
2
Figure 2.1
Note how the set of values 0 to 9 is repeated, and for each repeat, the column to the
left is incremented (from 0 to 1, then 2).
Each increase in value occurs, till the value of the largest number in the set is reached
(9), at which stage the next value is the smallest in the set (0) and a new value is
generated in the left column (i.e., the next value after 9 is 10).
09, 10 - 19, 20 - 29, 30 -39 etc
We always write the digit with the largest value on the left of the number
Base Values
The base value of a number system is the number of different values the set has before
repeating itself. For example, decimal has a base of ten values, 0 to 9.
• Binary = 2 (0, 1)
• Octal = 8 (0 - 7)
• Decimal = 10 (0 - 9)
• Hexadecimal = 16 (0 - 9, A-F)
Faculty of Information Technology - University of Moratuwa 59
Mathematics for Computing
Weighting Factor
The weighting factor is the multiplier value applied to each column position of the
number. For instance, decimal has a weighting factor of TEN, in that each column to
the left indicates a multiplication value increase of 10 over the previous column on the
right, i.e.; each column move to the left increases in a multiply factor of 10.
312 = 300 + 10 + 2
= 3 * 100 + 1 * 10 + 2 * 1
= 3 * 102 + 1 * 101 + 2 * 100
Weighting factors
2.2 Decimal system [Base-10]
This number system uses TEN different symbols to represent values. The set values
used in decimal are
0123456789
With 0 having the least value and nine having the greatest value. The digit or column
on the left has the greatest value, whilst the digit on the right has the least value.
When doing a calculation, if the highest digit (9) is exceeded, a carry occurs which is
transferred to the next column (to the left).
Example of addition and exceeding the base set range
8+4
8
9 +1
10 +2 Note 1:
11 +3
12 +4
Note1: When 9 is exceeded, we go back to the beginning of the set (0), and carry a
value of 1 over to the next column on the left.
Another example of addition and exceeding the base set range
198 + 4
198
199 +1
200 +2 Note 2:
201 +3
202 +4
Note2: When 9 is exceeded, we go back to the beginning of the set (0), and carry a
value of 1 over to the next column on the left. Thus the middle column (9) has 1
added to it, the next value in the set is 0, and we carry 1 (because the set was
Faculty of Information Technology - University of Moratuwa 60
Mathematics for Computing
exceeded) to the next left column. Adding the carry value of 1 to 1 in the left most
column gives.
Positional Values [Units, Tens, Hundreds, Thousands etc Columns]
We probably got taught at school about positional values, in that columns represent
powers of 10. This is expressed to us as columns of ones (0 - 9), tens (groups of 10),
hundreds (groups of 100) and so on.
237 = (2 groups of 100) + (3 groups of 10) + (7 groups of 1)
= (100 + 100) + (10 + 10 + 10) + (1 + 1 + 1 + 1 + 1 + 1 + 1)
= (200) + (30) + (7)
= 237
Each column move to the left is 10 times the previous value.
2.3 Binary system [Base-2]
The binary number system uses TWO values to represent numbers. The values are,
• 0
• 1
With 0 having the least value, and 1 having the greatest value. Columns are used in
the same way as in the decimal system, in that the left most column is used to
represent the greatest value.
As we have seen in the decimal system, the values in the set (0 and 1) repeat, in both
the vertical and horizontal directions.
00
01
10 Note: go to value lowest in set, carry to left
11
100 Note: go to value lowest in set, carry to left
101
110 Note: go to value lowest in set, carry to left
111
In a computer, a binary variable capable of storing a binary value (0 or 1) is called a
bit.
In the decimal system, columns represented multiplication values of 10. That was
because there were 10 values (0 - 9) in the set. In this binary system, there are only
two values (0 - 1) in the set, so columns represent multiplication values of 2.
1011 =
---- 1 * 20 = 1
----- 1 * 21 = 2
------ 0 * 22 = 0
------- 1 * 23 = 8
----
11 (in decimal)
Faculty of Information Technology - University of Moratuwa 61
Mathematics for Computing
Using n bits we can represented 2n number of binary values.
e.g.
If n = 8, then number of different values = 28 = 256
Rules for Binary Addition
Operation Result
0+0 0
0+ 1 1
1+0 1
1+1 0 and Carry 1
Table 2.3-1
1011 + 101 =
1011
101
1. Start at the right most column and apply the rules.
2. 1 + 1 is 0 and carry 1 onto the next column on the left
1011
101
------
0 and carry 1
Which really looks like
1011
101
1
------
0
3. Now consider the second column
4. 1 + 0 + 1 = ( 1 + 0 ) + 1 = 1 + 1 = 0, carry 1 onto the next column on the
left
1011
101
1
------
00 and carry 1
Faculty of Information Technology - University of Moratuwa 62
Mathematics for Computing
Which really looks like
1011
111
1
------
00
5. Now consider the third column
6. 0 + 1 + 1 = ( 0 + 1 ) + 1 = 1 + 1 = 0, carry 1 onto the next column on the
left
1011
111
1
------
000 and carry 1
Which really looks like
1011
111
1
------
000
7. Now consider the last column on the left
8. 1 + 1 is 0 and carry 1 to the left.
1011
101
------
10000
Rules for Binary Subtraction
Operation Result
0-0 0
0-1 1 and borrow 1
1-0 1
1-1 0
Table 2.3-2
Faculty of Information Technology - University of Moratuwa 63
Mathematics for Computing
Rules for Binary Multiplication
Operation Result
0*0 0
0*1 0
1*0 0
1*1 1
Table 2.3-3
Conversion from Decimal to Binary
Example: consider the decimal number 254.
Method 1: Divide the number by 2, then divide remainder by 2, and so on until
remainder equal zero. Write down the remainder (which is either 0 or 1) at each
division stage. Once there are no more divisions, list the remainder values in reverse
order. This is the binary equivalent.
Example 1
254 / 2 giving 127 with a remainder of 0 LSB
127 / 2 giving 63 with a remainder of 1
63 / 2 giving 31 with a remainder of 1
31 / 2 giving 15 with a remainder of 1
15 / 2 giving 7 with a remainder of 1
7/2 giving 3 with a remainder of 1
3/2 giving 1 with a remainder of 1
1/2 giving 0 with a remainder of 1 MSB
Thus the binary equivalent is 11111110
Example 2
132 / 2 giving 66 with a remainder of 0
66 / 2 giving 33 with a remainder of 0
33 / 2 giving 16 with a remainder of 1
16 / 2 giving 8 with a remainder of 0
8/2 giving 4 with a remainder of 0
4/2 giving 2 with a remainder of 0
2/2 giving 1 with a remainder of 0
1/2 giving 0 with a remainder of 1
thus the binary equivalent is 10000100
Conversion from Decimal to Binary
Each column represents a power of 2, so use this as a basis of calculating the number.
It is sometimes referred to as the 8:4:2:1 approach.
Faculty of Information Technology - University of Moratuwa 64
Mathematics for Computing
Write down the binary number. Where a 1 appears in the column, add the column
value as a power of 2 to a total.
Weighting 8 4 2 1 Answer
Binary Value 1 0 1 1 11
Table 2.3-4
Weighting 8 4 2 1 Answer
Binary Value 0 1 1 1 7
Table 2.3-5
Weighting 32 16 8 4 2 1 Answer
Binary Value 1 1 1 0 1 1 59
Table 2.3-6
Weighting 32 16 8 4 2 1 Answer
Binary Value 1 0 1 0 1 0 42
Table 2.3-7
Usually, binary numbers are represented with the suffix 2.
e.g.: 1001112
2.4 Octal Number System [Base-8]
The octal number system uses EIGHT values to represent numbers. The 8 distinct
values are,
01234567
With 0 having the least value and seven are having the greatest value. Columns are
used in the same way as in the decimal system, in that the left most column is used to
represent the greatest value. As we have seen in the decimal system, the values in the
set (0 and 1) repeat, in both the vertical and horizontal directions.
0 - 7, 10 -17, 20 - 27, 30 - 37 ......
Octal numbers are represented with a suffix 8
e.g.: 1768
Faculty of Information Technology - University of Moratuwa 65
Mathematics for Computing
Conversion from Octal to Decimal:
Problem: Convert 1768 to decimal.
Each column represents a power of 8,
176 = 1 * 82 + 7 * 81 + 6 * 80
= 1 * 64 + 7 * 8 + 6 * 1
= 64 + 56 + 6
= 126
Conversion from binary to Octal:
Problem: Convert 10110111 to octal.
Each octal digit is represented by 3 binary bits. Split the binary number
into groups of 3 bits, starting from the right.
10 110 111
=2 =6 =7
=267 in octal
Conversion from decimal to octal:
Problem: Convert 300 decimal to octal. Use the same method used earlier to divide
decimal to binary, but divide by 8.
00 / 8 = 37 with a remainder of 4
37 / 8 = 4 with a remainder of 5
= 4548
2.5 Hexadecimal Number System [Base-16]
The hexadecimal number system uses SIXTEEN values to represent Numbers. The 16
distinct values are, 0 1 2 3 4 5 6 7 8 9 A B C D E F, where A = 10, B = 11, .., F =
15.
With ‘0’ having the least value and ‘F’ having the greatest value. Columns are used in
the same way as in the decimal system, in that the left most column is used to
represent the greatest value. As we have seen in the decimal system, the values in the
set repeat, in both the vertical and horizontal directions.
0 -- F
10 --1F
20 --2F
30 --3F
Hexadecimal is often used to represent memory addresses in computer systems.
Faculty of Information Technology - University of Moratuwa 66
Mathematics for Computing
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Table 2.5-1
Hexadecimal numbers are represented with the suffix 16.
e.g.: 17616, A8C16, …
Conversion from hexadecimal to decimal:
Problem: Convert 17616 in hexadecimal to decimal.
Each column represents a power of 16,
176 = 1 * 162 + 7 * 161 + 6 * 160
= 1 * 256 + 7 * 16 + 6 * 1
= 256 + 112 + 6
= 374
Conversion from binary to hexadecimal:
Problem: Convert 10110 to hexadecimal.
Each hexadecimal digit represents 4 binary bits. Split the binary number
into groups of 4 bits, starting from the right.
Faculty of Information Technology - University of Moratuwa 67
Mathematics for Computing
1 0110
=1 =6
=16 in hexadecimal
Conversion from decimal to hexadecimal:
Convert 232 decimal to hexadecimal. Use the same method used earlier to divide
decimal to binary, but divide by 16.
232 / 16 = 14 with a remainder of 8
14 / 16 = 0 with a remainder of E (14 decimal = E)
= E816
2.6 Data Structures
Bits, Bytes, Nibbles and Word Length
Bit
A bit is the smallest element of information used by a computer? A bit holds ONE of
TWO possible values,
Value Meaning
0 OFF
1 ON
Table 2.6-1
A bit which is OFF is also considered to be FALSE or NOT SET; a bit which is ON is
also considered to be TRUE or SET. Because a single bit can only store two values,
bits are combined together into large units in order to hold a greater range of values.
Nibble
A nibble is a group of FOUR bits. This gives a maximum number of 16 possible
different values.
2 4 = 16 (2 to the power of the number of bits)
It is useful, when dealing with groups of bits, to determine which bit of the group has
the least value, and which bit has the most or greatest value. The Least Significant Bit
AND The Most Significant BitThis is deemed to be bit 0, and is always drawn at the
extreme right. The Most significant bit is always shown on the extreme left, and is the
bit with the greatest value.
The diagram below shows a NIBBLE, and each bits position and decimal weight
value (for more information, consult the module on Number Systems).
Faculty of Information Technology - University of Moratuwa 68
Mathematics for Computing
3 2 1 0 Bit Number
+---+---+---+---+
| | | | |
+---+---+---+---+
8 4 2 1 Decimal Weighting Value
MSB LSB
Bytes
Bytes are a grouping of 8 bits. This comprises TWO nibbles, as shown below.
7 6 5 4 3 2 1 0 Bit Number
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
128 64 32 16 8 4 2 1 Decimal Weighting Value
MSB LSB
Bytes are often used to store CHARACTERS. They can also be used to store numeric
values,
0 to 255 or
+127 to -128
Words
(…)
2.7 ASCII, BCD and EBCDIC Codes
ASCII (The American Standard Code for Information Interchange)
ASCII is a computer code which uses 128 different encoding combinations of a group
of seven bits (27 = 128) to represent,
• characters A to Z, both upper and lower case
• special characters, < . ? : etc
• numbers 0 to 9
• special control codes used for device control
Faculty of Information Technology - University of Moratuwa 69
Mathematics for Computing
Lets now look at the encoding method. The table below shows the bit
combinations required for each character.
Table 2.7-1
A computer usually stores information in eight bits. The eighth bit is unused in
ASCII, thus is normally set to 0. Some systems may use the eight bit to implement
graphics or different language symbols, i.e., Greek characters. Control codes are used
in communications and printers. They may be generated from an ASCII keyboard by
holding down the CTRL (control) key and pressing another key (A to Z, plus {, \, ], ^,
<- ). Example Code the text string 'Hello.' in ASCII using hexadecimal digits.
H = 48
e = 65
l = 6C
l = 6C
o = 6F
. = 2E
Faculty of Information Technology - University of Moratuwa 70
Mathematics for Computing
thus the string is represented by the byte sequence 48 65 6C 6C 6F 2E
2.8 BCD (Binary Coded Decimal)
In this code structure, each of the decimal digits (0-9) is represented by a four-bit
binary code as illustrated in Table 2.8-1. Recall that with 4 bits the number of unique
representations is 24=16. Thus there are 6 invalid four-bit combinations for the BDC
code.
Decimal BCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Invalid Combinations
1010
1011
1100
1101
1110
1111
Table 2.8-1
Converting decimal to BCD:
Faculty of Information Technology - University of Moratuwa 71
Mathematics for Computing
A) 546 10
B) 23.8 10
C) 5139 10
A) 5 = 0101, 4 = 0100, 6=0110 thus 546 = (010101000110) BCD
B) 23.8 = (00100011.1000) BCD
C) 5139 = (0101000100111001) BCD
Converting BCD codes to decimal:
To convert BCD codes to decimal, break the binary digits into groups of four starting
from the binary point and convert each group into its appropriate BCD digit.
A) 001110000111
B) 01100111.0100
A) (001110000111) BCD = 0011 1000 0111 = (387) 10
B) (01100111.0100) BCD = 0110 0111. 0100 =(67.4) 10
2.9 EBCDIC (Extended Binary – Coded Decimal Interchange Code)
Alphanumeric characters are stored in a computer using a character-encoding system
known as a collating sequence, where one or two bytes represents a given character.
The two single-byte character-encoding systems that are most widely used in data
processing are ASCII and EBCDIC. IBM mainframe computers use the EBCDIC
system. The EBCDIC system can be used to represent 256 different characters. Each
character is assigned a unique hexadecimal value between 00 and FF.
EBCDIC Code: Commonly Used Characters
Hex Character Hex Character Hex Character Hex Character
'40'x space '95' n 'C4' D 'E3' T
'4B' . '96' o 'C5' E 'E4' U
'4E' + '97' p 'C6' F 'E5' V
'60' - '98' q 'C7' G 'E6' W
'81' a '99' r 'C8' H 'E7' X
'82' b 'A2' s 'C9' I 'E8' Y
Faculty of Information Technology - University of Moratuwa 72