Lecture02-Data Representation 2
Lecture02-Data Representation 2
1
Overview
• Last Lecture
– Data representation 1
• This Lecture
– Data representation 2
• Positional notation
• Positive integers
• Negative integers
• Real numbers
2
Number Systems
• Numeral / Digit - a single symbol representing a quantity
– In the case of the switches there are 2 (0 and 1)
– In the case of “normal math” there are 10 (0,1,2,3,4,5,6,7,8,9)
3
Base 10 Numbers
4
Binary Number System
5
Decimal to Binary Conversion
6
Decimal to Binary Conversion (cont)
.687510 = .10112
8
Octal Number System
• Binary numbers get long quickly and so we group bits together and
use a larger base. 2 bits = base 4, 3 bits = base 8, 4 bits = base
16.
Bin Oct
• The base 8 system (octal) uses the symbols: 0, 1, 2, 3, 4, 5, 6, 7. 000 0
- It takes 3 binary bits to make an octal digit. 001 1
- Group the binary digits in groups of 3 from the decimal point (the Least 010 2
Significant Bit (LSB)). 011 3
100 4
101 5
• Octal gained popularity because some machines (such as the 110 6
PDP-8) used 12-bit, 24-bit or 36-bit words and numbers could be 111 7
easily displayed in 4, 8, and 12 (octal) digit displays. It was a cost
and complexity saving. It is also easy to learn.
9
Hexadecimal Number System
• Today machines use 8-bit, 16-bit, 32-bit or 64-bit words.
Using base 16 makes more sense. In hexadecimal (or hex)
each digit is 4 bits. To make up the “missing” digits letters are
used:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
11
Hexadecimal Number System (cont)
• Take the binary number, e.g.:
100110110011111
octal
decimal binary
hex
13
Introduction to Negative Integers
• So far we have concentrated on positive integers
14
Excess Notation
By convention we count from 0, but what if we count from a different number. Excess-N notation shifts all
values by N. That is, in excess-N notation, the number represented by a binary code is N less than the
unsigned value you would normally assign to that code. If we count from -3 (excess 3) then:
Number Bits
-3 000
-2 001
-1 010
0 011
1 100
2 101
3 110
4 111
Example 1:
The computer stores integer values in 8 bits, using excess 128 notation.
How is -6010 stored?
Answer: stored number = -60 + 128
= 6810 15
= 0100 01002
Excess Notation (cont)
Example 2:
The binary number, 01011101, is in excess 128 notation. What is the number
in decimal?
Answer:
0101 1101 = 0 + 64 + 0 + 16 + 8 + 4 + 0 + 1
= 93 (excess 128)
decimal number = 93 - 128
= -35
16
Sign Magnitude
17
One’s Complement
• Negative numbers, are represented by taking the one's complement
(inversion, negation) of the unsigned positive number.
84 = 0101 0100
-84 = 1010 1011
• Negation is easy
–Invert the bit-pattern
18
Two’s Complement
Decimal Binary
-4 100
-3 101
-2 110
-1 111
0 000
1 001
2 010
3 011
19
Example
11 ← carries
1010 1011 ← each bit complemented
+ 1
---------
1010 1100 ← -84 in two’s complement
20
Two’s Complement to Decimal
• If the high (sign) bit is 0
– Convert the binary number to decimal
21
Example
• Convert the 8-bit number 1110 1010 in two’s complement to
decimal
Complement and add 1
1 <-- carry
0001 0101 <-- complement each bit
+ 1 <-- add 1
---------
0001 0110
00010110= 16 + 4 + 2 = 22
= -22
22
Subtraction Example
• Subtract 5 from 18 in 8-bit, 2's complement
18 – 5 = 18 + -5
18 = 0001 0010
-5 = 1111 1011
----------
10000 1101
23
Two’s Complement Example 2
• Subtract 5 from -18 in 8-bit, 2's complement
-18 - 5 = -18 + -5
24
Real Numbers
• In Computer Science real numbers are often called floating
point numbers
25
Scientific Notation (decimal)
• If you move the decimal point to the left:
– You increase the exponent. 12,345.6 = 0.012345 =
• If you move the decimal point to the right: 12,345.6 x 100 0.012345 x 100
– You decrease the exponent.
1,234.56 x 101 0.12345 x 10-1
123.456 x 102 1.2345 x 10-2
Remember: 10 1
0
10
p
Remember: 20 1 110.001 x 22
1 11.0001 x 23
2 p
p
2 1.10001 x 24 26
Floating Point
27
IEEE Floating Point
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is
a technical standard for floating-point computation which was
established in 1985 by the Institute of Electrical and
Electronics Engineers (IEEE).
s e-127
(-1) x 1.f x 2
• Where
• s = sign (0 for positive, 1 for negative) of the number
• e = power of two, excess 127 notation (exponent)
• Allowed values for e are 0-255
• f = binary fraction (mantissa, significand)
29
Decimal to IEEE Conversion
1. Convert the decimal number to binary
2. Write the binary number in scientific notation base 2
3. Write f by taking the fractional part of the normalised
number and adding trailing zeroes to get 23 bits
4. Determine sign bit, s
5. Add 127 to the exponent (from step 2) to get e
6. Convert e to an 8 bit binary number (add leading
zeroes if needed)
7. Write in IEEE format by concatenating s, e, and f
30
Decimal to IEEE Conversion Example
• Convert 24.510 to IEEE floating point representation:
s e-127
= 1.10001 * 24 (-1) x 1.f x 2
31
Decimal to IEEE Conversion Example (cont.)
3. Write f by taking the fractional part of the normalised
number and adding trailing zeroes to get 23 bits
24.510 = 1.10001 * 24
f = 10001
Positive number so s = 0
32
Decimal to IEEE Conversion Example (cont.)
5. Add 127 to the exponent (from step 2) to get e
e = 4 + 127 = 131 How should 4 be stored?
= 1000 0011
33
IEEE to Decimal Conversion
1. Group the binary digits into 1, 8, and 23 digits (s,e,f)
2. Convert e to decimal
• Subtract 127 to get exp
3. Delete the trailing zeroes from f and write:
• 1.f x 2exp where the exp is the value from step 2 and f is the original f with
the trailing zeroes removed
4. Un-normalise the number by moving the binary point until
the exp = 0
5. Convert the binary number to decimal
6. If s is 1, negate the number
34
IEEE to Decimal Conversion
• Convert C1C4000016 to decimal form
s e f
35
IEEE to Decimal Conversion (cont.)
36
IEEE to Decimal Conversion (cont.)
= 1.10001 x 24
37
IEEE to Decimal Conversion (cont.)
38