Numerical Data
Representation
Dr. Khaled Wassif
Fall 2020-2021
SCS111
Outline
◼ Data Representation
◼ Numbering Systems
◼ Numbering Conversions
◼ Binary Operations
◼ Signed Fixed Point Numbers
◼ r’s and (r-1)’s Complements
◼ Floating Point Numbers
CS111 Introduction to Computers Slide 2- 2
By Dr. Khaled Wassif
Data Representation
◼ Data is represented in a computer system using the
binary system as strings of binary digits (bits).
– Each bit has two possible values (1 or 0).
◼ In the computer, the 0 can be represented by the
electronic current being off and the 1 by the
electronic current being on.
◼ Any typed letter on the keyboard is automatically
converted into a series of 0s and 1s that the computer
can recognize.
◼ Because humans don't use bit strings, hence
conversion must be implemented.
CS111 Introduction to Computers Slide 2- 3
By Dr. Khaled Wassif
Numbering Systems
◼ The two primary numbering systems used in
conjunction with computer are binary and
decimal.
◼ Decimal is translated into binary on input and
binary is translated into decimal on output.
◼ The octal and hexadecimal numbering systems
are used in reading and reviewing binary
output in the form of a memory dump.
CS111 Introduction to Computers Slide 2- 4
By Dr. Khaled Wassif
Popular Numbering Systems
System Radix (Base) Digits
Binary 2 0, 1
Octal 8 0, 1, 2, 3, 4, 5, 6, 7
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Hexadecimal 16
A, B, C, D, E, F
CS111 Introduction to Computers Slide 2- 5
By Dr. Khaled Wassif
Decimal Number System
Position …. 3 2 1 0 • -1 -2 ….
10Position 103 102 101 100 • 10-1 10-2
Value 1000 100 10 1 • 0.1 0.01
2 5 6 9 • 3 4
4 x 0.01 = 0.04
3 x 0.1 = 0.3
9x1= 9
6 x 10 = 60
5 x 100 = 500
2 x 1000 = 2000
2569.34
CS111 Introduction to Computers Slide 2- 6
By Dr. Khaled Wassif
R-ary Number System
◼ A number can be written using any number system as
follows:
an an-1 … a1 a0 • a-1 a-2 … a-m
◼ These equals to:
anrn + an-1rn-1 + … + a1r1 + a0r0 + a-1r-1 + a-2r-2 + … + a-mr-m
◼ The radix is represented as “r” and each digit aj is an
integer between 0 and (r-l).
◼ “•” is called the radix point.
– It is the decimal point in decimal system (with base 10), while
the binary point in binary system (with base 2).
CS111 Introduction to Computers Slide 2- 7
By Dr. Khaled Wassif
Numbering Conversions
◼ Conversion to Decimal Numbers:
– Conversion of a number with radix r to an equivalent decimal
number is easy.
– The conversion rules can be summarized as follows:
» Convert each digit of the number according to the position
value.
» Then, add them up.
– From binary system (base 2)
(101.011)2 = 1 x 22+ 0 x 21 + 1 x 20 + 0 x 2-1 + 1 x 2-2 + 1 x 2-3
= 4 + 0 + 1 + 0 + 0.25 + 0.125
= (5.375)10
CS111 Introduction to Computers Slide 2- 8
By Dr. Khaled Wassif
Numbering Conversions (cont.)
– From octal system (base 8)
(762.3)8 = 7 x 82 + 6 x 81 + 2 x 80 + 3 x 8-1
= 448 + 48 + 2 + 0.375
= (498.375)10
– From hexadecimal system (base 16)
(2B7)16 = 2 x 162 + 11 x 161 + 7 x 160
= 512 + 176 + 7
= (695)10
– From other system
(23.4)5 = 2 x 51 + 3 x 50 + 4 x 5-1
= 10 + 3 + 0.8 = (13.8)10
CS111 Introduction to Computers Slide 2- 9
By Dr. Khaled Wassif
Numbering Conversions (cont.)
◼ Conversion from Decimal Numbers:
– The following rules are applied to convert a decimal number to a
number of base r:
» Split integer part form fractional part.
» Construct integer digits by repeat dividing the integer part over r and
collect the remainder until nothing left to divide.
» Construct fraction by repeat multiplying the fractional part by r and
collect the integer part of the result until nothing left in its fractional part.
– To binary system (base 2)
(5.375)10 Integer Base Remainder Fraction Base Result
5 2 1 0.375 2 0.75
2 2 0 0.75 2 1.5
. 1 2 1 0.5 2 1.0
= (101.011)2 0 0.0
CS111 Introduction to Computers Slide 2- 10
By Dr. Khaled Wassif
Numbering Conversions (cont.)
(6.2)10 = (110.00110011)2 Integer Base Remainder Fraction Base Result
6 2 0 0.2 2 0.4
3 2 1 0.4 2 0.8
1 2 1 0.8 2 1.6
0 0.6 2 1.2
0.2 2 ……
– To octal system (base 8)
(498.375)10 = (762.3)8 Integer Base Remainder Fraction Base Result
498 8 2 0.375 8 3.0
62 8 6 0.0
7 8 7
0
– To hexadecimal system (base 16)
– To other system
CS111 Introduction to Computers Slide 2- 11
By Dr. Khaled Wassif
Numbering Conversions (cont.)
◼ Conversion from Radix rl To Radix r2:
– If rl and r2 are power of two (as octal and hexadecimal)
Convert each digit to base 2 and then regroup the resulted bits;
starting from the LSB; according to the following tables:
Octal Binary Hexadecimal Binary Hexadecimal Binary
0 000 0 0000 8 1000
1 001 1 0001 9 1001
2 010 2 0010 A 1010
3 011 3 0011 B 1011
4 100 4 0100 C 1100
5 101 5 0101 D 1101
6 110 6 0110 E 1110
7 111 7 0111 F 1111
Example: (36B9.A)16 = 0011 0110 1011 1001 . 1010
0 011 011 010 111 001 . 101 0 = (33271.5)8
CS111 Introduction to Computers Slide 2- 12
By Dr. Khaled Wassif
Numbering Conversions (cont.)
– If both rl and r2 are not power of two:
Convert from base rl to base 10 as an intermediate step, and then
convert from base 10 to r2
Example:
(3014)5 = 3 x 53 + 0 x 52 + 1 x 51 + 4 x 50
= 375 + 0 + 5 + 4 = (384)10
Number Base Remainder
384 11 10
34 11 1
3 11 3
0
(3014)5 = (384)10 = (31A)11
CS111 Introduction to Computers Slide 2- 13
By Dr. Khaled Wassif
Binary Operations
◼ Binary Addition:
– Binary addition is performed in the same manner as decimal
addition.
– The complete rules for binary addition is as follows:
0+0=0
0+1=1
1+0=1
1 + 1 = 0 with a carry over of 1
– Example:
1001 + 11111 = Carry 1 1 1 1 1
Augend 1 0 0 1
Addend + 1 1 1 1 1
Result 1 0 1 0 0 0
CS111 Introduction to Computers Slide 2- 14
By Dr. Khaled Wassif
Binary Operations
◼ Binary Subtraction:
– Subtraction is the inverse operation of addition.
– Normal procedure of binary subtraction is as follows:
0–0=0
1–0=1
0 – 1 = 1 with a borrow of 1 from the next higher order digit position
1–1=0
– Example:
2
11011 – 1101 =
Borrow 0 0 2
Minuend 1 1 0 1 1
Subtrahend – 1 1 0 1
Result 0 1 1 1 0
CS111 Introduction to Computers Slide 2- 15
By Dr. Khaled Wassif
Binary Operations
◼ Binary Multiplication:
– The complete rules for binary multiplication is as follows:
0x0=0
0x1=0
1x0=0
1x1=1
– Example:
1101 x 110 = M'cand 1 1 0 1
M'plier x 1 1 0
(1) 0 0 0 0
(2) 1 1 0 1
(3) 1 1 0 1
Result 1 0 0 1 1 1 0
CS111 Introduction to Computers Slide 2- 16
By Dr. Khaled Wassif
Signed Fixed Point Numbers
◼ Signed Magnitude Representation:
– Used to represent signed numbers (Positive and Negative).
» For the positive number, the leftmost bit is the sign (0) and the
remaining bits are the magnitude.
» For the negative number the leftmost bit is the sign (1) and the
remaining bits are the magnitude.
– Example:
+8910 = 010110012
- 8910 = 110110012
– There are two representations for zero according to this
approach:
+0 = 000000002
- 0 = 100000002
– Using an 8-bit, the numbers are between -12710 and +12710.
CS111 Introduction to Computers Slide 2- 17
By Dr. Khaled Wassif
r's Complement
◼ For a positive number Nr in base r with an n-digit integer
part, the r's complement, rC(Nr), is given by:
rn – Nr if n > 0
rC(Nr) =
0 if n = 0
◼ Examples:
– The l0's complement of 458010 is:
10C(458010) = 104 – 458010 = 1000010 – 458010 = 542010
– The 10's complement of 318.6510 is:
10C(318.6510) = 103 – 318.6510 = 100010 – 318.6510 = 681.3510
– The 2‘s complement of 1.1012 is
2C(1.1012)= 2l – 1.1012 = 102 – 1.1012 = 0.0112
CS111 Introduction to Computers Slide 2- 18
By Dr. Khaled Wassif
(r-1)'s Complement
◼ For a positive number Nr in base r with an n-digit integer
part and an m-digit fractional part, the (r-l)'s complement
r-1C(Nr) is given by:
(r-1)C(Nr) = rn – r-m - Nr
◼ Examples:
– The 9's complement of 458010 (where n = 4 , m = 0) is:
9C(458010) = 104 – 10-0 – 458010 = 1000010 – 1 – 458010 = 541910
– The 9's complement of 318.6510 (where n = 3 , m=2) is:
9C(318.6510) = 103 – 10-2 – 318.6510 = 100010 – 0.01 – 318.6510 = 681.3410
– The 1‘s complement of 1.1012 (where n = 1 , m=3) is
1C(1.1012)= 2l – 2-3 – 1.1012 = 102 – 0.0012 – 1.1012 = 0.0102
CS111 Introduction to Computers Slide 2- 19
By Dr. Khaled Wassif
r's and (r-1)'s Complements
◼ The (r-1)'s complement is always less than the r's
complement by the value of the least significant digit.
◼ To find rC(Nr), we can find (r-l)C(Nr) and then add 1 to
the least significant digit.
◼ The 1's complement of a binary number is formed by
changing each 1 in the number to a 0 and each 0 to a 1.
◼ Example:
– The 2's complement of 1001.12 is
2C(1001.12) = 1111.12 – 1001.12 + 0.12 = 0110.12,
Since the 1's complement of 1001.102 is
1C(1001.12) = 1111.12 – 1001.12 = 0110.02
CS111 Introduction to Computers Slide 2- 20
By Dr. Khaled Wassif
Subtraction with r's Complement
◼ If r's complement is used to represent negative numbers, the
subtraction operation can formed by the following steps:
1. Form RI = M + rC(Nr).
2. If there is a nonzero carry out of the addition, discard that carry
and the remaining digits are the result (R is RI without last carry).
3. Otherwise, take the r's complement of RI, and attach a minus sign
in the front, i.e., the result R is - rC([RI]r).
◼ Example:
– 652410 - 237010 = 652410 + 763010 (where 763010 is the l0's complement of 237010)
6524 6524
-2370 +7630
4154 14154
Note the carry out of the leftmost digit is ignored and the result is 4154.
CS111 Introduction to Computers Slide 2- 21
By Dr. Khaled Wassif
Subtraction with r's Complement
◼ Other Examples:
– 237010 - 652410 = 2370 + 10C(6524l0)
(where 347610 is the l0's complement of 652410 )
2370 2370
- 6524 + 3476
- 4154 5846
Note there is no end carry, so take the 10‘s complement of 5846 (4154),
and attach a minus sign in front (i.e., the result is - 4154).
– 110112 – 11012 = 110112 + 2C(011012)
(where 100112 is the 2's complement of 011012 )
11011 11011
- 1101 + 10011
1110 101110
CS111 Introduction to Computers Slide 2- 22
By Dr. Khaled Wassif
Subtraction with (r-1)'s Complement
◼ If (r-1)'s complement is used to represent negative
numbers, the subtraction operation is formed by the
following steps:
1. Form RI = M + (r-1)C(Nr).
2. If an end carry occurs on the left, add this carry to the least
significant digit (called end around carry) to get the result.
3. Otherwise, the result R is -(r-1)C([RI]r)
◼ Example:
– 652410 - 237010 = 652410 + 762910 (where 762910 is the 9's complement of 237010)
6524 6524
-2370 +7629
4154 14153
Note the carry is add to the least significant digit and the result is 4154.
CS111 Introduction to Computers Slide 2- 23
By Dr. Khaled Wassif
Subtraction with (r-1)'s Complement
◼ Other Examples:
– 237010 - 652410 = 2370 + 9C(6524l0)
(where 347510 is the 9's complement of 652410 )
2370 2370
- 6524 + 3475
- 4154 5845
Note there is no end carry, so take the 9‘s complement of 5845 (4154), and
attach a minus sign in front (i.e., the result is - 4154).
– 110112 – 11012 = 110112 + 1C(011012)
(where 100102 is the 1's complement of 011012 )
11011 11011
- 1101 + 10010
1110 101101
CS111 Introduction to Computers 1 Slide 2- 24
By Dr. Khaled Wassif 1110
Floating Point Numbers
◼ Floating point numbers allow very large and very
small numbers to be represented using only a few
digits, at the expense of precision.
◼ The precision is established by the number of digits
in the fraction which determine the number of digits in the
exponent.
◼ Example:
– (+8.175 X l016)
+ 1 6 8 • 1 7 5
CS111 Introduction to Computers Slide 2- 25
By Dr. Khaled Wassif
Floating Point Numbers (cont.)
◼ Normalization
– The decimal number 375 can be represented in different
floating point forms as
375 x 100,or
37.5 x 101, or
Problem: many representations
3.75 x102, or
for the same number.
.375 x I03, or
.0375 x 104, ……
– Hence, floating point numbers are usually normalized, in
which the radix point is located in only one possible position
for a given number.
» Usually, but not always, the normalized representation places the radix
point immediately to the left of the leftmost, nonzero digit in the
fraction, as in: .375 x 103.
CS111 Introduction to Computers Slide 2- 26
By Dr. Khaled Wassif
IEEE-754 Floating Point Formats
CS111 Introduction to Computers Slide 2- 27
By Dr. Khaled Wassif
SUMMARY
◼ Data Representation
◼ Numbering Systems
◼ Numbering Conversions
◼ Binary Operations
◼ Signed Fixed Point Numbers
◼ 1’s and 2’s Complements
◼ Floating Point Numbers
CS111 Introduction to Computers Slide 2- 28
By Dr. Khaled Wassif