Chapter 2 : Number System
SSK3207 – Chapter 6
*
Chapter 6
Number System
SAK3207 – Chapter 6
*
Topics
6.1 Decimal, Binary, Octal and Hexadecimal Numbers
6.2 Relation between binary number system with other number system
SAK3207 – Chapter 6
*
6.3 Representation of integer, character and floating point numbers in binary
6.4 Arithmetic Operations for Ones Complement, Twos Complement, magnitude and sign and floating point number
SSK3207 – Chapter 6
*
6.1) Decimal, Binary, Octal and Hexadecimal Numbers
Introduction
Most numbering system use positional notation :
N = anrn + an-1rn-1 + … + a1r1 + a0r0
Where: N: an integer with n+1 digits
r: base
ai {0, 1, 2, … , r-1}
SSK3207 – Chapter 6
*
Example:
a) r = 10 (base 10) => decimal numbers
symbol : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (10 different symbols)
N = 278 => n = 2; a2 = 2; a1 = 7; a0 = 8
= (2 x 102) + (7 x 101) + (8 x 100)
Hundreds
Ones
Tens
SSK3207 – Chapter 6
*
b) r = 2 (base-2) => binary numbers
symbol: 0, 1 (2 different symbols)
N = 10012 => n = 3; a3 = 1; a2 = 0; a1 = 0; a0 = 1
= (1 x 23) + (0 x 22) + (0 x 21) + (1 x 20)
c) r = 8 (base-8) => Octal numbers
symbol : 0, 1, 2, 3, 4, 5, 6, 7, (8 different symbols)
N = 2638 => n = 2; a2 = 2; a1 = 6; a0 = 3
= (2 x 82) + (6 x 81) + (3 x 80)
SSK3207 – Chapter 6
*
d) r = 16 (base-16) => Hexadecimal numbers
symbol : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (16 different symbols)
N = 26316 => n = 2; a2 = 2; a1 = 6; a0 = 3
= (2 x 162) + (6 x 161) + (3 x 160)
SSK3207 – Chapter 6
*
There are also non-positional numbering systems.
Example: Roman Number System
1987 = MCMLXXXVII
Decimal
Binary
Octal
Hexadecimal
0
0
0
0
1
1
1
1
2
10
2
2
3
11
3
3
4
100
4
4
5
101
5
5
6
110
6
6
7
111
7
7
8
1000
10
8
9
1001
11
9
10
1010
12
A
11
1011
13
B
12
1100
14
C
13
1101
15
D
14
1110
16
E
15
1111
17
F
16
10000
20
10
SSK3207 – Chapter 6
*
6.2) Relation between binary number system and others
6.2.1) Binary and Decimal
Converting a decimal number into binary
Integers:
Divide the decimal number by 2 and take its remainder
The process is repeated until it produces the result of 0
The binary number is obtained by taking the remainder from the bottom to the top
*
SSK3207 – Chapter 6
*
Example:
5310 => 53 / 2 = 26 remainder 1
26 / 2 = 13 remainder 0 13 / 2 = 6 remainder 1
6 / 2 = 3 remainder 0 3 / 2 = 1 remainder 1
1 / 2 = 0 remainder 1
1101012 (6 bits) = 001101012 (8 bits)
(note: bit = binary digit)
Read the remainders from the bottom to the top
Fractions:
The fraction part in binary notation can be represented by:
0.b-1b-2b-3….. , where bi = 0 or 1
and has the value:
(b-1 x 2-1) + (b-2 x 2-2) + (b-3 x 2-3) + ….
Examples:
0.10112 = (0x20)+(1×2-1)+(0x2-2)+(1×2-3)+(1×2-4)
11.0112 = (1×21)+(1×20)+(0x2-1)+(1×2-2)+(1×2-3)
SAK3207 – Chapter 6
*
Decimal to Binary
multiply the decimal fraction by two and take the value of the integer part.
use the fraction part of the product as the multiplicand in the step.
the process is repeated until to the approximate value needed.
SAK3207 – Chapter 6
*
Examples:
SAK3207 – Chapter 6
*
SSK3207 – Chapter 6
*
2. Converting a binary number into decimal
Integers:
Multiply each bit in the binary number with the weight (or position value)
Add up all the results of the multiplication performed
The desired decimal number is the total of the multiplication results performed
SSK3207 – Chapter 6
*
Example:
a) 1110012 (6 bits)
= (1×25) + (1×24) + (1×23) + (0x22) + (0x21) + (1×20)
= 32 + 16 + 8 + 0 + 0 + 1
= 5710
b) 000110102 (8 bits)
= 24 + 23 +21
= 16 + 8 + 2
= 2610
Fractions:
Note: The steps are similar to the integers.
Example:
1) 0.10112 = (0x20)+(1×2-1)+(0x2-2)+(1×2-3)+(1×2-4)
= 0.0 + 0.5 + 0.25 + 0.125 + 0.0625
= 0.937510
2) 11.0112 = (1×21)+(1×20)+(0x2-1)+(1×2-2)+(1×2-3)
= 2 + 1 + 0 + 0.25 + 0.125
= 3. 37510
SAK3207 – Chapter 6
*
SSK3207 – Chapter 6
*
6.2.2) Binary and Octal
Theorem
If base R1 is the integer power of other base, R2, i.e. R1 = R2d
Every group of d digits in R2 is equivalent to 1 digit in the R1 base
(Note: This theorem is used to convert binary numbers to octal and hexadecimal or the other way round)
SSK3207 – Chapter 6
*
From the above theorem, assume that
R1 = 8 (base-8) octal
R2 = 2 (base-2) binary
If R1 = R2d 8 = 23
So, 3 digits in base-2 (binary) is equivalent to 1 digit in base-8 (octal)
SSK3207 – Chapter 6
*
From the stated theorem, the following is a binary-octal conversion table.
In a computer system, the conversion from binary to octal or otherwise is based on the conversion table above.
Binary
Octal
000
0
001
1
010
2
011
3
100
4
101
5
110
6
111
7
SSK3207 – Chapter 6
*
Example:
Convert the folowing binary numbers into octal numbers:
i) 001011112 (8 bits) ii) 111101002 (8 bits)
Refer to the binary-octal conversion table above
000 101 111
= 578
Refer to the binary-octal conversion table above
11 110 100
= 3648
0
5
7
3
6
4
SSK3207 – Chapter 6
*
b) Convert the following octal numbers into binary numbers (16 bits)
(a) 658 (b) 1238
Refer to the binary-octal conversion table above
68 58
110 101
= 0000 0000 001101012
Refer to the binary-octal conversion table above
18 28 38
001 010 011
= 00000000010100112
SSK3207 – Chapter 6
*
6.2.3) Binary and Hexadecimal
The same method that employed in binary-octal conversion is used.
Assume that:
R1 = 16 (hexadecimal)
R2 = 2 (binary)
from the theorem: 16 = 24 , hence, 4 digits in a binary number is equivalent to 1 digit in the hexadecimal number system (and otherwise)
SSK3207 – Chapter 6
*
Example:
Convert the binary number, 001011112 into hexadecimal number.
Refer to the binary-hexadecimal conversion table:
0010 11112 = 2F16
2 F
Binary
Hexadecimal
0000
0
0001
1
0010
2
0011
3
0100
4
0101
5
0110
6
0111
7
1000
8
1001
9
1010
A
1011
B
1100
C
1101
D
1110
E
1111
F
SSK3207 – Chapter 6
*
b) Convert the following hexadecimal numbers into binary numbers
(a) 12B16 (b) ABCD16
Refer to the binary-hexadecimal conversion table above
1 2 B16
0001 0010 10112
= 0001001010112
Refer to the binary-hexadecimal conversion table above
A B C D16
1010 1011 1101 11102
= 10101011110111102
SSK3207 – Chapter 6
*
6.3) Representation of integer, character and floating point numbers in binary
6.3.1) Introduction
Machine instructions operate on binary data. The most important general categories of data are:
1. Addresses – unsigned integer
2. Numbers – integer or fixed point, floating point numbers and decimal (eg, BCD (Binary Coded Decimal))
3. Characters – IRA (International Reference Alphabet), EBCDIC (Extended Binary Coded Decimal Interchange Code), ASCII (American Standard Code for Information Interchange)
4. Logical Data – only has 2 values either 1 or 0.
Those commonly used by computer users/programmers: signed integer, floating point numbers and characters
SAK3207 – Chapter 6
*
ASCII Code
SAK3207 – Chapter 6
*
SSK3207 – Chapter 6
*
6.3.2) Signed Integer Representation
Signed integers are usually used by programmers
Unsigned integers are used for addressing purposes in the computer (especially for assembly language programmers)
Three representations of signed integers:
1. Sign-and-Magnitude
2. Ones Complement
3. Twos Complement
SSK3207 – Chapter 6
*
a) Sign-and-Magnitude
The easiest representation
The leftmost bit in the binary number represents the sign of the number, 0 if positive and 1 if negative
The balance bits represent the magnitude of the number.
SSK3207 – Chapter 6
*
Examples:
i) 8 bits binary number
__ __ __ __ __ __ __ __
7 bits for magnitude (value)
a) (i) +7 = 0 0 0 0 0 1 1 1
Sign bit
0 => +ve
1 => –ve
Sign bit = 0 (+ve number)
Magnitude for 710 in 7-bit binary
SAK3207 – Chapter 6
*
b) (i) –10 = 1 0 0 0 1 0 1 0
(ii) +10 = 000010102
(ii) –7 = 100001112
Sign bit = 1 (-ve number)
Magnitude for 710 in 7-bit binary
Sign bit = 1 (-ve number)
Magnitude for 1010 in 7-bit binary
Magnitude for 1010 in 7-bit binary
Sign bit = 0 (+ve number)
SSK3207 – Chapter 6
*
ii) 6 bits binary number
__ __ __ __ __ __
5 bits for magnitude (value)
a) (i) +7 = 0 0 0 1 1 1 (ii) -7 = 1 0 0 1 1 1
Sign bit
0 => +ve
1 => –ve
Sign bit = 0 (+ve number)
Magnitude for 710 in 5-bit binary
Sign bit = 0 (+ve number)
Magnitude for 710 in 5-bit binary
SSK3207 – Chapter 6
*
b) Ones Complement
In the ones complement representation, positive numbers are same as that of sign-and-magnitude
Example: +5 = 00000101 (8 bit)
as in sign-and-magnitude representation
Sign-and-magnitude and ones complement use the same representation for all positive numbers.
Sign bit = 0 (+ve number)
Magnitude for 510 in 7-bit binary
SAK3207 – Chapter 6
*
For negative numbers, their representations are obtained by changing bit 0 → 1 and 1 → 0 from their positive number representations.
Example: Convert –5 into 8-bit ones complement representation.
Solution:
First, obtain +5 representation in 8 bits
00000101
Change every bit in the number from 0 to 1 and
vice-versa.
–510 in ones complement is 111110102
SSK3207 – Chapter 6
*
Exercise:
Get the representation of ones complement (6 bit) for the following numbers:
i) +710 ii) –1010
Solution:
(+7) = 0001112
Solution:
+1010 = 0 0 1 0 1 02
So, -1010 = 1 1 0 1 0 12
Sign bit = 0 (+ve number)
Magnitude for 710 in 5-bit binary
Change each bit 0 → 1 and 1 → 0
SSK3207 – Chapter 6
*
c) Twos complement
For +ve numbers their representations are similar to ones complement and sign-and-magnitude
For –ve numbers their representations are obtained by adding 1 to the ones complement of the numbers.
*
SSK3207 – Chapter 6
*
Example:
Convert –5 into twos complement representation and give the answer in 8 bits.
Solution:
v First, obtain +5 representation in 8 bits
000001012
v Obtain ones complement for –5
111110102
v Add 1 to the ones complement number:
111110102 + 12 = 111110112
v –5 in twos complement is 111110112
SSK3207 – Chapter 6
*
Exercise:
What are the representations of the following numbers in 6-bit twos complement?
i) +710 ii) –1010
Solution:
+7 = 0001112
Solution:
+1010 = 0010102
-1010 = 1101012 + 12
= 1101102
So, twos complement for –10 is 1101102
-1010 in ones complement
SSK3207 – Chapter 6
*
6.3.3) Character Representation
For character data type, its representation uses codes such as the ASCII, IRA or EBCDIC.
Note: Students are encouraged to obtain
the codes
SSK3207 – Chapter 6
*
6.3.4) Floating point representation
In computer, floating point numbers are represented in the form of:
+S x B+E
and the number can be stored in computer words with 3 fields:
i) Sign (+ve, –ve)
ii) Significand S
iii) Exponent E
where B is base and is implicit (need not be stored because it is the same for all numbers (base-2)).
Diagrams below are IEEE standard for binary floating-point known as IEEE Standard 754.
SAK3207 – Chapter 6
*
The characteristics of the three formats:
SAK3207 – Chapter 6
*
Below are examples of floating-point number with 8 bits for biased exponent (Binary32).
SAK3207 – Chapter 6
*
Biased Exponent
In IEEE 754 floating points numbers, the exponent is biased – the value stored is offset from the actual value by the exponent bias.
To calculate the bias for an arbitrary sized floating point number apply the formula 2(k-1) -1 where k is the number of bits allocated for an exponent field.
If k=8, the bias is 28-1 – 1 = 128 – 1 = 12710 = 11111112
Eg1: 1.1010001 x 210100
An exponent 101002 will convert to the biased exponent 100100112 (11111112 + 101002)
SAK3207 – Chapter 6
*
SSK3207 – Chapter 6
*
6.4) Arithmetic Operations for Ones Complement, Twos Complement, and sign-and-magnitude.
6.4.1) Introduction
Reminder: This chapter only focus on addition and subtraction only.
Most of the computer system will change the subtraction operation to the addition operation
Example: i) 8 – 5 = 8 + (–5)
ii) –10 + 2 = (–10) + 2
iii) 6 – (–3) = 6 + 3
SSK3207 – Chapter 6
*
6.4.2) Sign-and-Magnitude
Assume that the operation is:
Z = X + Y
There are a few possibilities:
i) If both numbers, X and Y are positive
Just perform the addition operation
Example: 510 + 310 = 0001012 + 0000112
= 0010002
= 810
SSK3207 – Chapter 6
*
ii) If both numbers are negatives
Add |X| and |Y| and set the sign bit = 1 to the result, Z
Example:
–310 – 410 = (–3) + (–4)
= 1000112 + 1001002
☺Only add the magnitude:
000112 + 001002 = 001112
Set the sign bit of the result (Z) to 1 (–ve)
= 1001112
= –710
SSK3207 – Chapter 6
*
iii) If signs of both numbers are differ
There will be 2 cases:
a) | +ve Number | > | –ve Number |
Example: (–2) + (+4), (+5) + (–3)
ð Set the sign bit of the –ve number to 0 (+ve), so that both numbers become +ve.
ð Subtract the number of smaller magnitude from the number with a bigger magnitude
SSK3207 – Chapter 6
*
Sample solution (in 6-bit):
(–2) + (+4)
Get the representation for each number:
(–2) = 1000102
(+4) = 0001002
Change the sign bit of the –ve number to +ve
(–2) change to (+2) = 0000102
Subtract (+2) from (+4)
= (+4) – (+2)
= 0001002 – 0000102
= 0000102 = 210
SAK3207 – Chapter 6
*
b) | –ve Number | > | +ve Number |
ð For this condition, just subtract the +ve number from the –ve number.
Example: (+310) + (–510)
ð Find the representation for each number
+310 = 0000112
–510 = 1001012
ð Subtract the +ve number (+3) from the –ve number (-5)
(-510) – (+310) = 1001012 – 0000112
= 1000102
= –210
SSK3207 – Chapter 6
*
6.4.3. Ones complement
In ones complement, it is easier than sign-and-magnitude:
ð Change the numbers into their representations and perform the addition operation
ð However a situation called Overflow might occurs when addition is performed on the following conditions:
1. If both are negative numbers
2. If both numbers are in difference sign and |+ve Number| > | –ve Number|
SSK3207 – Chapter 6
*
Overflow => the addition result exceeds the number of bits that was fixed
Condition 1: Both are –ve numbers
Eg: –310 – 410 = (–310) + (–410)
Solution: (8 bits)
ð Convert –310 and –410 into ones complement representations.
+410 = 000001002
–410 = 111110112
+310 = 000000112
–310 = 111111002
SSK3207 – Chapter 6
*
ð Perform the addition operation
–310 => 111111002 (8 bit)
+ – 410 => 111110112 (8 bit)
–710 1111101112 (9 bit)
Overflow occurs. This value is called EAC bit and needs to be added to the rightmost bit.
the answer (8 bits)
111101112
+ 12
111110002 = –710
SSK3207 – Chapter 6
*
Condition 2: | +ve Number| > |–ve Number|
Eg: (–2) + 4 = (–2) + (+4)
Solution: (8 bits)
ð Convert –210 and +410 into ones complement representation
–2 = 111111012 +4 = 000001002
ð Add both of the numbers
(–210) => 11111101 (8 bit)
+ (+410) => 00000100 (8 bit)
There is an EAC (overflow occurs)
+210 100000001 (9 bit)
SSK3207 – Chapter 6
*
ð Add the EAC to the rightmost bit
00000001
+ 1
000000102 = +210
the answer
Note:
For conditions other than 1 & 2 above, overflow does not occur and there will be no EAC (hence, no need to perform addition operation to the rightmost bit).
SSK3207 – Chapter 6
*
6.4.4. Twos Complement
Addition operation in twos complement is same with that of ones complement, i.e. overflow occurs if:
If both are negative numbers
If both numbers are in difference sign and |+ve Number| > |–ve Number|
SSK3207 – Chapter 6
*
Condition 1: Both numbers are –ve
Example: –310 – 410 = (–310) + (–410)
Solution: (6 bits)
ð Convert both numbers into twos complement representation
+310 = 0000112 (6 bit)
–310 = 1111002 (one’s complement)
–310 = 1111012 (two’s complement)
+410 = 0001002
–410 = 1110112 (one’s complement)
–410 = 1111002 (two’s complement)
SSK3207 – Chapter 6
*
111100 (–310)
111011 (–410)
= 1110012 (two’s complement)
= –710
ð Perform addition operation on both the numbers in twos complement representation and ignore the EAC bit.
1111001
Ignore the EAC bit
The answer
SSK3207 – Chapter 6
*
Note:
In twos complement, EAC is ignored (no need to be added to the leftmost bit, like that of ones complement)
SSK3207 – Chapter 6
*
Condition 2: | +ve Number| > |–ve Number|
Example: (–2) + 4 = (–2) + (+4)
Solution:
ð Convert both numbers into twos complement representation:
+2 = 0000102 +4 = 0001002
–2 = 1111012 (in ones complement)
–2 = 1111012 + 12
–2 = 1111102 (in twos complement)
SAK3207 – Chapter 6
*
ð Perform an addition operation on both numbers:
(–210) => 111110 (6 bit)
+ (+410) => 000100 (6 bit)
+210 1000010
Ignore the EAC
The answer is 0000102 = +210
SSK3207 – Chapter 6
*
Note: For cases other than condition 1 and 2 above, overflow does not occur.
Exercise:
Perform the following arithmetic operations in ones complement and twos complement.
1. (+2) + (+3) [6 bit]
2. (–2) + (–3) [6 bit]
3. (–2) + (+3) [6 bit]
4. (+2) + (–3) [6 bit]
Compare your answers with the stated theory.