MODULE 4: DATA REPRESENTATIONS IN
THE COMPUTER
Objectives:
At the end of the module, the students shall be able to:
1. Perform arithmetic operations for the different number systems
2. Represent signed numbers
3. Represent fractional numbers
4.1. Number Systems: decimal, binary, octal, hexadecimal
Human beings use decimal (base 10) number systems for counting and
measurements (probably because we have 10 fingers and two big toes). Computers use
binary (base 2) number system, as they are made from binary digital components (known
as transistors) operating in two states – on and off. In computing, we also use hexadecimal
(base 16) or octal (base 8) number systems, as a compact form to represent binary
numbers.
4.1.1. Decimal number system (Base 10)
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Using the positional notation, the least
significant digit (right-most digit) is of the order of 100 (units or ones), the second
right-most digit is of the order of 101 (tens), the third right-most digit is of the
order of 102 (hundreds), and so on.
For example: 73510 = 7 x 102 + 3 x 101 + 5 x 100
4.1.2. Binary number system (Base 2)
Bits: 0 and 1. Positional notation example, 101102 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21
+ 0 x 20
4.1.3. Octal number system (Base 8)
Octal digits: 0, 1, 2, 3, 4, 5, 6, and 7.
Positional notation example: 3418 = 3x82 + 4x81 + 1x80
4.1.4. Hexadecimal number system (Base 16)
Hex digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F
Positional notation example: A3E16 = 10x162 + 3x161 + 14x80
4.2. Number System Conversion
4.2.1. Converting Decimal to Binary, Octal and Hexadecimal
Integer Part (use Remainder method)
a. Divide the decimal number by the base of the target number system that is, to convert
decimal to binary, divide the decimal number with 2 (the base of binary number
system), 8 for octal and 16 for hexadecimal.
b. Note the remainder separately as the first digit from the right. In case of hexadecimal,
if the remainder exceeds 9, convert the remainder into equivalent hexadecimal form.
For e.g., if the remainder is 10 then note the remainder as A.
c. Continually repeat the process of dividing until the quotient is zero and keep writing
the remainders after each step of division.
d. Finally, when no more division can occur, write down the remainders in reverse order.
Fractional Part
1. Multiply the fractional part by the value of the new base (i.e. in 45.73 10 =0.73 x 2;
where 2 is the new base or target base)
2. Record the integer part if it exits, else record 0.
3. Repeat step 1 with the result of the previous multiplication and then step 2, until the
fractional part becomes 0. In case of infinite calculations, generally 6 digits are taken.
Example:
Convert 35.7510 to binary
Integer part (use remainder method)
35 / 2 = 17, Remainder 1
17 / 2 = 8, Remainder 1
8 / 2 = 4, Remainder 0
4 / 2 = 2, Remainder 0
2 / 2 = 1, Remainder 0
Fractional Part
0.75 x 2 = 1.5, Integer part = 1
0.5 x 2 = 1.0, Integer part = 1
Therefore: 35.7510 = 00011.112
4.2.2. Converting Binary, Octal and Hexadecimal to Decimal
The method used for conversion of a binary, octal and hexadecimal number to decimal
number involves each digit of the binary, octal or hexadecimal number to be multiplied
by its weighted position, and then each of the weighted values are added to get the
decimal number. Example:
Binary Number 1 1 0 1 0
Weight of each bit 24 23 22 21 20
Weighted Value 24 * 1 23 * 1 22 * 0 21 * 1 20 * 0
Solved Multiplication 16 8 0 2 0
Sum of weight of all bits = 16 + 8 +0 + 2 + 0 = 26
Thus, the decimal equivalent of (11010)2 is (26)10
Example:
a. Convert 10112 to Decimal number system
1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 8 + 0 + 2 + 1 = 1110
b. Convert 3568 to Decimal number system
3 x 82 + 5 x 81 + 6 x 80 = 192 + 40 + 6 = 23810
4.2.3. Converting Between Octal and Hexadecimal
Steps of conversion
a. Convert each octal digit to 3-bit binary form
b. Combine all the 3-bit binary numbers.
c. Segregate the binary numbers into 4-bit binary form by starting the first
number from the right bit (LSB) towards the number on the left bit (MSB).
d. Finally, convert these 4-bit blocks into their respective hexadecimal symbols.
Example:
Given 3568, convert to Hexadecimal number system
38 = 011
58 = 101
68 = 110
Therefore, 3568 = 0 1110 11102 = EE16
4.2.4. Arithmetic operations
a. Order of precedence of operators
If you are asked to simplify something like "4 + 2×3", the question that naturally arises
is "Which way do I do this? Because there are two options!" I could add first:
4 + 2×3 = (4 + 2) × 3 = 6×3 = 18
...or I could multiply first:
4 + 2×3 = 4 + (2×3) = 4 + 6 = 10
Which answer is the right one? Arithmetic operations follow the following order of
precedence:
1. Parentheses ()
2. Exponents
3. Multiplication and Division (from left to right)
4. Addition and Subtraction (from left to right)
b. Binary Addition and Subtraction
Binary addition is much like your normal everyday addition (decimal addition), except
that it carries on a value of 2 instead of a value of 10.
For example: in decimal addition, if you add 8 + 2 you get ten, which you write as 10;
in the sum this gives a digit 0 and a carry of 1. Something similar happens in binary
addition when you add 1 and 1; the result is two (as always), but since two is written
as 10 in binary, we get, after summing 1 + 1 in binary, a digit 0 and a carry of 1.
Therefore in binary:
0+0=0 1–1=0
0+1=1 1–0=1
1+0=1 0 – 1 = 1 (borrow 1)
1 + 1 = 10 (which is 0 carry 1) 0–0=0
Examples:
Add 23 and 52 Subtract 23 from 52
1 0111 11 0100
+ 11 0100 - 1 0111
100 1011 1 1101
c. Binary Multiplication
In the multiplication, the digits of the multiplier are stepped through one at a time,
from right to left. Each digit of the multiplicand is then multiplied, in turn, by the
current multiplier digit; taken together, these single-digit multiplications form a
partial product. The answer to each single-digit multiplication comes from the
multiplication table. Some of these answers are double-digit numbers, in which case
the least significant digit is recorded and the most significant digit is carried over to
be added to the result of the next single-digit multiplication.
Example: 1011.01 x 110.1
1 0 1 1.0 1
x 1 1 0.1
101101
000000
101101
1 0 1 1 0 1 __
1 0 0 1 0 0 1.0 0 1
d. Binary Division
Binary division is almost as easy to calculate as multiplying binary numbers, and
involves our knowledge of binary multiplication. Be sure to review binary
multiplication first.
Example: Divide 11001 / 11
a. 0 b. 01 c. 010
11/1101 11/1101 11/1101
0 0 0
1 11 11
Note:
1/11 = 0 ;since 1<11 11 11
0 00
Note:
11/11 = 1 00
0
Note:
00/11 = 0
d. 0100
11/1101
0
11
11
00
00
01
Note:
00/11 = 0
Thus, 1100 / 11 = 01002 and Remainder 1
e. Octal Division
Divide 34438 and 78
Step 1: Construct a table of 7 and its multiples
Decimal Octal
7x1 7 7
7x2 14 16
7x3 21 25
7x4 28 34
7x5 35 43
7x6 42 52
7x7 49 61
7x8 56 70
7x9 63 77
Step 2: Divide
4 0 5
7/3 4 4 3
3 4 ; 4 8 x 78
0 4 ; 04 is less than 8, thus, quotient is 0
0 ; 0 8 x 78
4 3
4 3 ; 58 x 78
0
4.2.5. Represent Signed Numbers
Positive numbers (including zero) are represented as unsigned numbers without
the need to put the + sign in front of them to show that they are positive numbers.
However, when dealing with negative numbers we do use a - sign in front of the number
to show that the number is negative in value and different from a positive unsigned value,
and the same is true with signed binary numbers.
However, in digital circuits there is no provision made to put a plus or even a minus
sign to a number, since digital systems operate with binary numbers that are represented
in terms of “0’s” and “1’s”.
a. Sign-magnitude data representation
Given 5310, represent in 8-bit using sign-magnitude data representation
Given -5310, represent in 8-bit using sign-magnitude data representation
b. One’s complement (only for negative signed numbers)
Given -5310, what is its 1’s complement data representation?
10110101 = 11001010
c. Two’s Complement (only for negative signed numbers)
Given -5310, what is its 2’s complement data representation?
10110101 = 11001011
d. Floating-Point Number Representation
A floating-point number (or real number) can represent a very large (1.23×10^88) or
a very small (1.23×10^-88) value. It could also represent very large negative number
(-1.23×10^88) and very small negative number (-1.23×10^88), as well as zero, as
illustrated:
A floating-point number is typically expressed in the scientific notation, with a fraction
(F), and an exponent (E) of a certain radix (r), in the form of F×r^E. Decimal numbers
use radix of 10 (F×10^E); while binary numbers use radix of 2 (F×2^E).
Representation of floating point number is not unique. For example, the number
55.66 can be represented as 5.566×10^1, 0.5566×10^2, 0.05566×10^3, and so on. The
fractional part can be normalized. In the normalized form, there is only a single non-
zero digit before the radix point. For example, decimal number 123.4567 can be
normalized as 1.234567×10^2; binary number 1010.1011B can be normalized as
1.0101011B×2^3.
It is important to note that floating-point numbers suffer from loss of precision when
represented with a fixed number of bits (e.g., 32-bit or 64-bit). This is because there
are infinite number of real numbers (even within a small range of says 0.0 to 0.1). On
the other hand, a n-bit binary pattern can represent a finite 2^n distinct numbers.
Hence, not all the real numbers can be represented. The nearest approximation will
be used instead, resulted in loss of accuracy.
It is also important to note that floating number arithmetic is very much less efficient
than integer arithmetic. It could be speed up with a so-called dedicated floating-point
co-processor. Hence, use integers if your application does not require floating-point
numbers.
In computers, floating-point numbers are represented in scientific notation of fraction
(F) and exponent (E) with a radix of 2, in the form of F×2^E. Both E and F can be
positive as well as negative. Modern computers adopt IEEE 754 standard for
representing floating-point numbers. There are two representation schemes: 32-bit
single-precision and 64-bit double-precision.
IEEE-754 32-bit Single-Precision Floating-Point Numbers
In 32-bit single-precision floating-point representation:
The most significant bit is the sign bit (S), with 0 for positive numbers and 1 for
negative numbers.
The following 8 bits represent exponent (E).
The remaining 23 bits represents fraction (F).
Normalized Form
Suppose that the 32-bit pattern is 1 1000 0001 011 0000 0000 0000 0000 0000, with:
Step 1: Find the sign S
S=1
Step 2: Find the exponent E
E = 1000 0001
Step 3: Find the fraction F
F = 011 0000 0000 0000 0000 0000
In the normalized form, the actual fraction is normalized with an implicit leading 1 in
the form of 1.F. In this example, the actual fraction is 1.011 0000 0000 0000 0000 0000
Step 4: Actual fraction
1x2^0 + 0x2^-1 + 1×2^-2 + 1×2^-3 = 1.37510.
The sign bit represents the sign of the number, with S=0 for positive and S=1 for
negative number.
Step 5: Sign
In this example with S=1, this is a negative number, thus, -1.37510.
In normalized form, the actual exponent is E-127 (so-called excess-127 or bias-127).
This is because we need to represent both positive and negative exponent. With an
8-bit E, ranging from 0 to 255, the excess-127 scheme could provide actual exponent
of -127 to 128.
Step 6: Actual Exponent
E-127=129-127=210
Step 7: Number represented is -1.375×2^2=-5.510.
Represent Fractional numbers
Example 1: Given -5.510, show its single precision floating point data representation
Step 1: Convert decimal in its binary representation (use remainder method and etc.)
5.510 = 101.1
Step 2: Show the binary number in its normal form
101.1 = 1.011 (the decimal point is moved 2 bits to the left)
Step 3: Identify the sign, exponent and mantissa
Sign: 1 (since -5.5 is negative)
Exponent: 2 (the # of times the decimal point is moved) + 127 (biased exponent)
= 129 = 10000001
Mantissa: 011 + pad with 0's to fill in the full 23 bits
= 01100000000000000000000
Step 4: Show in its 32-bit floating point representation
Sign + Exponent + Mantissa
1 10000001 01100000000000000000000
Example 2: Given 52.72510, show its single precision floating point data representation
Step 1: 52.72510 = 110100.10111002 ;1100 is repeating
Step 2: 110100.10111002 1.1010010111002
Step 3: Identify the sign, exponent and mantissa
Sign: 0 (since 52.72510 is positive)
Exponent: 5 + 127 = 132 = 10000100
Mantissa: 101001011100 ̅̅̅̅̅̅̅
Step 4: Show in its 32-bit floating point representation
Sign + Exponent + Mantissa
0 10000100 101001011100 1100 1100 110
NAME: ________________________________ COURSE/YR: _________________
ASSESSMENT: (Show your solutions at the back of this answer sheet)
1. Add the following:
11001112 + 10011112 = ________________________
2348 + 6548 = ________________________
5ECD2516 + EEDC16 = ________________________
1. Subtract the following:
110112 – 011002 = ________________________
3468 – 2358 = ________________________
5EC6616 – 5233A16 = ________________________
2. Multiply the following:
10012 x 1102 = ________________________
2348 x 2278 = ________________________
3. Divide the following
10/100112 = ________________________
5/7568 = ________________________
A/FACE16 = ________________________
4. Convert to one’s complement:
10102 = ________________________
111100002 = ________________________
101000012 ________________________
5. Convert to two’s complement:
10102 = ________________________
111100002 = ________________________
100000002 = ________________________
0111111112 = ________________________
6. Convert these negative decimal values to negative binary using two’s
complement:
-19210 = ____________________________________
-1610 = ________________________
-110 = ________________________
7. Given 78.7510, show its single precision floating point data representation