Ch4-Machine Level Representation of Data-2019

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

CS 114: Discrete Structures

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.

 Types of Positional Number System


• Binary number system
• Decimal number system
• Octal number system
• Hexadecimal number system
3
Number Systems

A bit is a digit in binary numbers.


• It is the basic unit of information in a computer.
• It is the smallest unit of measurement
• It is a state of on or off in a digital circuit.
• We use 1 or 0 to represent the state.

Bits can be grouped together for


larger range of numbers.
• Example: 2 switches are used to represent 4 values:
(00), (01), (10) and (11).
4
Number Systems

A byte is a group of eight bits.


• It is the smallest possible addressable unit of computer storage.
• Used to represent one character.
A word is a contiguous group of bytes.
• Words can be any number of bits or bytes.
• Word sizes of 16, 32, or 64 bits are most common.
• Use consecutive bytes to store large data. bits
• e.g. 4 bytes = 1 word

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

 Humans cannot work well with large binary numbers.


• Memory addresses and other data can be quite large.

 Bits can be grouped together to represent other types


of number systems.
• Octal digit = 3 bits.
 It uses 3 bits to generate 8 symbols.
 It is also known as base 8 .

• Hexadecimal digit = 4 bits.


 It use s4 bits to generate 16 symbols.
 It is also known as base 16.

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.

 Approaches to store numbers:


1.Fixed point numbers: fixed number of digits after the point.
• Example: 41.687
2.Floating point numbers:a varying number
of digits after the point.
• Example: 1.11001 × 27
9
Outline

 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

 In fixed point numbers, each number has two


parts: an integer part and a fractional part.
(a3a2 a1a0 .a1a2 ) r 
a3  r 3  a2  r 2  a
 For converting, we start at the point (radix point).
• The integer part is converted from right to left.
• The fractional part is converted from left to right

12
Conversion from Base b to Decimal
(a3a2 a1a0 .a1a2 ) r 
 Method
(a3a2 a1a0 .a1a2 ) r  a3  r 3  a2  r 2  a1  r1  a0  r 0  a1  r 1  a2  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

= 1*8 + 0*4 + 1*2+ 1*1


=8+0+2+1
= 11
(101.101)2 = 1x22 + 0x21 + 1x20 + 1x2-1 + 0x2-2 + 1x2-3
= 5.625
Question: (101011)2 = (?)10
Question: (11010.11)2 = (?)10
13
Conversion from Base b to Decimal

2. Octal number system (base = 8)


(4271)8 = 4*83 + 2*82 + 7*81 + 1*80
= 4*512 + 2*64 + 7*8 + 1*1
= 2048 + 128 + 56 + 1
= 2233

Question: (724)8 = (?)10

14
Conversion from Base b to Decimal

3. Hexadecimal number system (base = 16)


(4A1C)16 = 4*163 + A*162 + 1*161 + C*160
= 4*4096 + 10*256 + 1*16 + 12*1
= 16384 + 2560 + 16 + 12
= 18972

Question: (ABC)16= (?)10


Question: (4021.23)5 = (?)10

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

Question: (125)10 = (?)2


Question: (70)10 = (?)2
17
Conversion from Decimal to Binary
(0.3125)10 = (0.0101)2

Question: (41.6875)10 = (?)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.

 To check the previous example:


(0.3125)10 = (0.0101)2
by multiply the answer (0.0101) 2 by 2:
= 0x20 + 0x2-1 + 1x2-2 + 0x2-3+1x2-4
= (0.3125)10 Correct!

19
Conversion from Decimal to Octal
(1234)10 = (2322)8

(139.6875)10 = (213.54)8

 Question: (467)10 = (?)8


20
Conversion from Decimal to Hexadecimal

(422)10 = (1A6)16

Question: (1234)10 = (?)16


Question: (3564.875)10 = (?)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

Replace each octal digit by the 3 corresponding


binary digits. You can suppress Leading 0s.
Example
(352)8 = (011 101 010)2 = (11101010)2

Question: (705)8 = (?) 2

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)

2. Double Precision (64 bits)


 sign (1 bit), exponent (11 bits), fraction (52 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

So the presentation is:


0 1000 0110 110 0100 0000 0000 0000 0000
32
Floating Point Representation
Example2: Present -110110011.01 in 32 bit floating
point.

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

So the presentation is:


0 1000 0111 101 1001 1010 0000 0000 0000
33
Floating Point Representation
Example3: Present 101.111001 in 32 bit floating point.

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

So the presentation is:


0 1000 0001 011 1100 1000 0000 0000 0000
Question: (- 111010)2 = (?) 34
Outline

 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

 Both signed and unsigned numbers are represented as a


string of bits (0s and 1s).
 Even the sign should be represented with 0 or 1.

 In unsigned numbers, all bits are used to represent the


number including the most significant bit (MSB).
 Example: The binary numbers below are unsigned, the
equivalent decimals are:
 00101001 = (41)10
 10101001 = (169)10
37
Signed Binary Numbers

 The sign bit is the most significant bit (MSB) represents

the sign of the number.

 The sign bit is 0 for positive (+) and 1 for negative (-).
 Signed numbers can be represented in different
representations:
1. Sign-Magnitude representation.

2. Signed-1’s - complement representation

3. Signed-2’s - complement 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

2’s complement of base 2:


• Leave all least significant 0’s and first 1 unchanged and
then replace each 0 by 1 and each 1 by 0.
Example: 2’s complement of 1011000 = 0101000.
Example: 2’s complement of 11010010 = 00101110.
Question: 2’s Complement of 0101101 = (?)2
39
Signed Binary Numbers
Sign-Magnitude Representation
 This notation consists of a magnitude and a sign.
 The leftmost bit represents the sign and the rest
represents the number itself(magnitude).
 In negate a number, the sign bit is 1.
 It’s used in ordinary arithmetic.
 Examples: assuming Signed-Magnitude
 (+9)10 = (01001)2
1 1 0 0 1
 (-9 )10 = (11001)2

 Question: assuming Signed-Magnitude Sign bit Magnitude

 (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

• Example: (123)10 =(0001 0010 0011)BCD

 In general, coding requires more bits than


conversion.
 A number with N decimal digits is coded with 4N
bits in BCD
• Example: (123)10 has 3 decimal digits and 12 bits in BCD

46

You might also like