4 - Machine Level Representation of Data-Latest
4 - Machine Level Representation of Data-Latest
CHAPTER 4:
MACHINE LEVEL REPRESENTATION OF DATA
Outline
Number systems.
Numeric data representation.
Decimal Numbers
Binary Numbers
Octal Numbers
Hexadecimal Numbers
Conversion between different numbering System
Arithmetic Operations in Binary System
Signed and Unsigned numbers
Addition
Subtraction using 2’s Complement
Overflow
Floating point representation
2
Number Systems
Number systems can be categorized into: Decimal Roman
2 II
1. Non-Positional Number System 20 XX
• Symbol represents the value regardless of its position. 200 CC
• Example: Roman numbers: I, II, III, IV, V, VI, VII, VIII, IX, X.
2. Positional Number System
• Symbols represent different values depending upon the position.
• Example: Decimal numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
1 1 00 1 1 0 1 000 1 1 1 0 1 1 00 1 00 1 0
1 2 3 4...
bytes 5
Number Systems
6
Number Systems
Any number can be written in base b, using b digits
If b = 10 we have decimal with 10 digits
If b = 2 we have binary with 2 digits
If b = 8 we have octal with 8 digits
If b = 16 we have hexadecimal with 16 digits
Name b
b=10
Note:
b=2
Base b is called radix r.
b=8
b=16
7
Number Systems
8
Number Systems
In binary terms:
• Most significant bit (MSB) is the left-most bit that has the
greatest effect on the number
• Least significant bit (LSB) is the right-most bit.
9
Conversion between Bases
10
Conversion from Base r to Decimal
Method
(a3a2 a1a0 .a1a 2 ) r
1 2
a3 b a2 r a1 r a0 r a1 r a 2 r
3 2 1 0
13
Exercises
14
Conversion from Decimal to Base r
Method
Step 1:- If the number has radix point, then it is
necessary to separate the number into an integer
part and a fractional part.
Step 2:- For integer part, divide it and its all
successive quotients by r until new quotient
becomes zero, and accumulate the remainders in
the reverse order.
Step 3:- For fractional part, multiply it and its all
successive fractional parts by r until new
fractional part becomes zero, and accumulate the
integer parts in the order.
15
Conversion from Decimal to Binary
(41.6875)10 = (?)2
Integer Remainder Integer Fraction
41 0.6875x 2 = 1 + 0.375
20 1 0.375 x 2 = 0 + 0.75
10 0 0.75 x 2 = 1 + 0.5
5 0 0.5 x2= 1 +0
2 1
0.1011
1 0 101001 answer
0 1 answer
16
Conversion from Decimal to Octal
(234.513)10 = (?)8
Integer Remainder Integer Fraction
234 0.513 x 8 = 4 + 0.104
29 2 0.104 x 8 = 0 + 0.832
3 5 0.832 x 8 = 6 + 0.656
0 3 0.656 x 8 = 5 + 0.248
0.248 x 8 = 1 + 0.984
0.40651…..
352 answer
answer
17
Conversion from Decimal to Hexadecimal
(234.513)10 = (?)16
Integer Remainder Integer Fraction
234 0.513 x 16 = 8 + 0.208
14 A 0.208 x 16 = 3 + 0.328
0 E 0.328 x 16 = 5 + 0.248
0.248 x 16 = 3 + 0.968
0.968 x 16 = F + 0.488
0.8353F…..
EA
answer
answer
18
Conversion from Binary to Octal
Binary
Number
Unsigned Signed
Positive and Negative
Only Positive Numbers numbers
23
Unsigned Binary Numbers
10101001 = (169)10
24
Signed Binary Numbers
The sign bit is 0 for positive (+) and 1 for negative (-).
representations:
Sign-Magnitude representation.
26
Signed Numbers
1’s-Compelement Representation
Positive numbers are represented as it is like signed positive number
(unsigned number).
Just like signed-magnitude, the (MSB) represents the sign bit.
To get the negation of a number, all bits are inverted including the sign
bit.
Example 1: use 1’s complement to find the negative of the number (+9)
(+9)10 = (01001)2
(-9)10 = (10110)2
Example 2: Assuming 1’s complement, what is the binary
representation of (-19)10 in 8-bits
(19)10 = (00010011) 2 , (-19)10 = (11101100) 2
Example 3: Assuming 1’s complement, what is the decimal number of
(11111101)2
It’s a negative number since (MSB) is 1
(11111101)2 = ( -2 )10 27
Signed Numbers
2’s-Compelement Representation
Positive numbers are represented as it is like signed positive number (unsigned
number).
Just like signed-magnitude, the (MSB) represents the sign bit.
To get the negation of a number, all bits are inverted including the sign bit;
then, add 1.
Example 1: use 2’s complement to find the negative of the number (+9)
(+9)10 = (01001)2
(-9)10 = (10110)2 + (1) 2 = (10111) 2
Example 2: Assuming 2’s complement, what is the binary
representation of (-19)10 in 8-bits
(19)10 = (00010011) 2 , (-19)10 = (11101100) 2 + (1) 2 = (11101101) 2
Example 3: Assuming 2’s complement, what is the decimal number of
(11111101)2
It’s a negative number since (MSB) is 1, take the 2’s complement, convert to decimal
and add (-) sign
(11111101)2 = ( -3 )10 28
Signed Binary Numbers
29
Binary Addition
Z 1 1 1 1
X 0 0 1 1
Carry in (Z) of 1: +Y +0 +1 +0 +1
CS 01 10 10 11 30
Adding Unsigned Binary Numbers
31
Subtracting Unsigned Binary Numbers
using 2’s complement
The subtraction of two n-digit unsigned binary
numbers M-N using 2’s complement can be done as
follows:
1. Add the minuend, M, to the 2’s complement of the
subtrahend, N.
2.If M N, the end carry is discarded;
3.If M < N, there is no end carry. Then take the 2’s
complement of the result and place a negative
sign in front.
32
Subtracting Unsigned Binary Numbers
using 2’s complement
33
Adding Signed Binary Numbers
34
Adding Signed Binary Numbers
Ex:
Adding two negative numbers (-6)+(-13)
represented in 8-bits
(-6) is represented in 2’s complement
(+6) = (00000110), (-6) = (11111010)
(-13) is represented in 2’s complement
(+13) = (00001101), (-13) = (11110011)
(-6) 11111010
Carry out
(-13) 11110011 discarded
(-19) 111101101
35
Adding Signed Binary Numbers
Ex:
Adding two negative numbers (-6)+(+13)
represented in 8-bits
(-6) is represented in 2’s complement
(+6) = (00000110), (-6) = (11111010)
(+13) = (00001101)
(-6) 11111010
Carry out
(+13) 00001101 discarded
(+7) 100000111
36
Adding Signed Binary Numbers
Ex:
Adding two negative numbers (+6)+(-13)
represented in 8-bits
(+6) = (00000110)
(-13) is represented in 2’s complement
(+13) = (00001101), (-13) = (11110011)
(+6) 00000110
(-13) 11110011
(-7) 11111001
37
Subtracting Signed Binary Numbers
( A) ( B ) ( A) ( B )
( A) ( B ) ( A) ( B )
38
Subtracting Signed Binary Numbers
( A) ( B ) ( A) ( B )
( A) ( B ) ( A) ( B )
39
Overflow
If we start with two n-bit numbers and the sum occupies n
+ 1 bits, we say that an overflow occurs.
Unsigned numbers
perform (9) + (8) in 4-bits
(1001) + (1000) = (10001), ends up with 5-bit, therefore, an overflow
occurs.
The final result is (0001) in 4-bits which is less than the correct result
Signed numbers (2’s complement)
It occurs when two numbers with the same sign are added and the result
has different sign.
Perform (-5) + (-6) in 4-bits
(5) = (0101), (-5)=(1011), (6)=(0110), (-6)=(1010)
(1011) + (1010) = (10101) discard the last bit then the final result (0101)
The result is positive while the two numbers are negative, then, an overflow
occurs.
40
Floating Point Representation
Single Precision
31 30 23 22 0
S Exponent Significant (Mantissa)
43
Floating Point Representation
64 bit floating-point representation:
1. Convert a number to: S * 1.fraction * 2(Exp)
The first bit (the left of the binary point) is always 1.
2. Compute the three components:
Sign= 0 or 1
0: for positive number. 1: for negative number.
Exponent= (Exp+ 1023 )
Example: if Exp=7, Exponent=(7 + 1023)=(1030)= 10000000110
Example: if Exp=−4, Exponent=(−4 + 1023)=(1019)= 01111111011
Fraction= fraction+the rest is 0s
44
Floating Point Representation
Example1: Present +11100100 in 32 bit floating point.
Answer:
1. +11100100 = + 1.11001 × 27
2. Compute the three components:
Sign = 0 (positive number)
Exponent = (7 + 127)2=(134 )2 = (1000 0110)2
Fraction = 11001+the rest is 0s
Answer:
1. -110110011.01 = -1.1011001101 * 28
2. Compute the three components:
Sign = 1 (negative number)
Exponent = (8 + 127)2= (135)2 =(1000 0111)2
Fraction = 1011001101+the rest is 0s
Answer:
1. 101.111001 = +1.01111001 * 22
2. Compute the three components:
Sign = 0 (positive number)
Exponent = (2 + 127)2= (129)2 =(1000 0001)2
Fraction = 01111001 +the rest is 0s