Ch4-Machine Level Representation of Data-2019
Ch4-Machine Level Representation of Data-2019
Ch4-Machine Level Representation of Data-2019
CHAPTER 4:
MACHINE LEVEL REPRESENTATION OF DATA
Outline
Number systems.
Numeric data representation.
• Unsigned numbers
Integer and Fixed point systems
Conversion between bases
Floating point systems
• Signed numbers
BCD code
2
Number Systems
Number systems can be categorized into: Decimal Roman
2 II
1. Non-Positional Number System
20 XX
• Symbols represent values 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 on their 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.
Number systems.
Numeric data representation.
• Unsigned numbers
Integer and Fixed point systems
Conversion between bases
Floating point systems
• Signed numbers
BCD code
10
Conversion between Bases
• We can convert a number (a unsigned Integer and
Fixed point number) between any two bases.
Methods summary:
1. ×I ×F ÷I ×F
Base b Decimal Decimal Base b
2. group 3 group 4
Binary Octal Binary Hexadecimal
Hexadecimal Octal
Binary
11
Conversion between Bases
12
Conversion from Base b to Decimal
(a3a2 a1a0 .a1a2 ) r
Method
(a3a2 a1a0 .a1a2 ) r a3 r 3 a2 r 2 a1 r1 a0 r 0 a1 r 1 a2 r 2
1 2
3
1.aBinary
r 3
a 2
Systemr 2
a1
(base r 1
= 2)
a 0 r 0
a 1 r a 2 r
3
(1011)2 = 1*2 + 0*2 + 1*2 + 1*22 1 0
14
Conversion from Base b to Decimal
15
Conversion from Decimal to Base b
Method
1. If the number has radix point, then separate it
into an integer part and a fractional part.
2. For integer part, divide it by r until it becomes
zero, and accumulate the remainders in the
reverse order.
3. For fractional part, multiply it by r until it
becomes zero, and accumulate the integer parts
in the order.
16
Conversion from Decimal to Binary
(37)10 = (100101)2
18
Conversion from Decimal to Binary
Note: to check if the answer (of base b) is correct,
we reconvert it to Decimal by multiply it by b.
19
Conversion from Decimal to Octal
(1234)10 = (2322)8
(139.6875)10 = (213.54)8
(422)10 = (1A6)16
21
Conversion from Binary to Octal
1. Partition binary number into groups of 3 digits
starting from the radix point
2. Replace each group of 3 binary digits by the
correspondent octal digit
3. If the number of digits is not a multiple of 3, add 0s to
left (in integer part) or right (fraction) the radix point.
Example
•(1011010111)2 = (1 011 010 111)2
= (1 3 2 7)8
•(11101010.1111)2=(011 101 010 . 111 100)2
= (352.74)8
Question: (110111101100.111)2 = (?) 8
22
Conversion from Octal to Binary
23
Conversion from Binary to Hexadecimal
1. Partition binary number into groups of 4 digits
starting from the radix point.
2. Replace each group of 4 binary digits by the
correspondent hexadecimal digit.
3. If the number of digits is not a multiple of 4, add 0s
to left integer part or right fraction part.
Example
(111101010.111001)2=(0001 1110 1010 . 1110 0100)2
= (1EA.E4)16
Question: (110111101100.111)2 = (?) 16
24
Conversion from Hexadecimal to Binary
Replace each hexadecimal digit by the 4
corresponding binary digits. You can suppress
Leading 0s.
Example
(EA)16 = (1110 1010)2 = (11101010)2
Question: (10AF)16 = (?)2
26
Conversion from Octal to Hexadecimal
For conversion from octal to hexadecimal, convert
first to binary, then from binary to hexadecimal
(Vice versa)
Example
(352)8 = (011 101 010)2 = (11101010)2
(11101010)2 = (1110 1010)2 = (EA)16
(352)8 = (EA)16
Question:
(1076)8 =(?) 16
27
Conversion from Hexadecimal to Octal
For conversion from hexadecimal to octal, convert
first to binary, then from binary to octal
Example
(EA)16 = (1110 1010)2 = (11101010)2
(11101010)2 = (011 101 010)2 = (352)8
(EA)16 = (352)8
Question: (1F0C)16 =(?)8
28
Outline
Number systems.
Numeric data representation.
• Unsigned numbers
Integer and Fixed point systems
Conversion between bases
Floating point systems
• Signed numbers
BCD code
29
Floating Point Representation
IEEE Standard 754 Floating Point Numbers
Three components:
1. sign. 2.exponent. 3. fraction (mantissa)
Two formats:
1. Single Precision (32 bits)
sign (1 bit), exponent (8 bits), fraction (23 bits)
30
Floating Point Representation
32 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+ 127 )2
Example: if Exp=7, Exponent=(7 + 127)2=(134)2= 10000110
Example: if Exp=−4, Exponent=(−4 + 127)2=(123)2= 1111011
Fraction= fraction+the rest is 0s
31
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
Number systems.
Numeric data representation.
• Unsigned numbers
Integer and Fixed point systems
Conversion between bases
Floating point systems
• Signed numbers
BCD code
36
Binary Numbers
The sign bit is 0 for positive (+) and 1 for negative (-).
Signed numbers can be represented in different
representations:
1. Sign-Magnitude representation.
38
Signed Binary Numbers
1’s complement of base 2:
• Replace each 0 by 1 and each 1 by 0.
Example: 1’s complement of 1011000 = 0100111 .
Example: 1’s complement of 0101101 = 1010010.
Question: 1’s Complement of 11010010 = (?)2
Question: 1’s Complement of 100110 = (?)2
(1101)2 = ( )10
(0 100 0001)2 = ( )10
(1000)2 = ( )10
(-19)10 = ( )2 (Use 8 bits)
40
Signed Binary Numbers
Signed-1’s-Compelement Representation
Positive numbers are represented as in sign-magnitude.
To get the negative of a number
1. Find the binary representation of the corresponding positive number
( DO NOT Forget sign bit)
2. Take 1’s complement including the sign bit.
The complement starts with 1 indicating a negative number.
Examples: assuming signed- 1’s complement:
(+9)10 = (01001)2 1 0 1 1 0
(-9)10 = (01001)1’s2 = (10110)2
Sign bit Magnitude
Question: assuming signed- 1’s complement:
(1101)2 = ( )10
(0 100 0001)2 = ( )10
(1000)2 = ( )10
(-19)10 = ( )2 (Use 8 bits)
41
Signed Binary Numbers
Signed-2’s-Compelement Representation
Positive numbers are represented as in sign-magnitude.
To get the negative of a number
1. Find the binary representation of the positive number ( DO NOT Forget
sign bit)
2. Take 2’s complement including the sign bit.
The complement starts with 1 indicating a negative number.
Note: 2’s complement of (0000)= ( 0000) in binary system
Examples: assuming signed- 2’s complement:
(+9)10 = (01001)2 1 0 1 1 1
(-9)10 = (01001)2’s2 =(10111)2
Question: assuming signed- 1’s complement: Sign bit Magnitude
(1101)2 = ( )10
(0 100 0001)2 = ( )10
(1000)2 = ( )10
(-19)10 = ( )2 (Use 8 bits)
42
Signed Binary Numbers
43
Outline
Number systems.
Numeric data representation.
• Unsigned numbers
Integer and Fixed point systems
Conversion between bases
Floating point systems
• Signed numbers
BCD code
44
Binary-Coded Decimal(BCD)
An encoding for each decimal digit.
It uses four bits to represent one decimal digit.
A decimal digit in BCD is the same as its
equivalent binary digit (only between 0 and 9).
• The binary numbers 1010 (i.e. (10)10) through 1111 (i.e.
(15)10) are not used and have no meaning in BCD.
For greater numbers, each digit is represented
by its own binary sequence.
• Example: (12)10 =(0001 0010)BCD
• Example: (4267)10 = (0100 0010 0110 0111)BCD
• Example: (9803)10 = (1001 1000 0000 0011)BCD
• Question: (249)10 = (?)BCD
45
Binary-Coded Decimal(BCD)
Conversion or Coding?
There is a different between conversion of a decimal number to a
binary number and coding a decimal number with a BCD.
• Example: (123)10= (01111011)2
46