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
• The base 10 system has 10 symbols (numerals / digits):
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3810 = 1001102 6
Decimal to Binary Conversion (cont)
• Convert 38.687510 to binary (base 2)
.687510 = .10112
7
• So, 38.6875 = 100110.10112
Binary to Decimal Conversion (cont)
• Working outward from the decimal point:
100110.1011
= 1*25 + 0*24 + 0*23 + 1*22 + 1*21 + 0*20 + 1*2-1 + 0*2-2 + 1*2-3 + 1*2-4
= 32 + 0 + 0 + 4 + 2 + 0 +1/2 + 0 + 1/8 + 1/16
= 32 + 0 + 0 + 4 + 2 + 0 + (0.5 + 0 + 0.125 + 0.0625)
= 38.6875
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.
Bit (LSB)).
• Octal gained popularity because some machines (such as the PDP-8) used 12-bit,
24-bit or 36-bit words and numbers could be easily displayed in 4, 8, and 12 (octal)
digit displays. It was a cost and complexity saving. It is also easy to learn.
Bin Oct
258=010 1012 =2110 000 0
001 1
Or
010 2
011 3
Multiply each digit of the octal number by the corresponding power of eight: 100 4
2x81 + 5x80 101 5
= 2x8 + 5x1 110 6
= 16+5 111 7
= 21
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
10
Hexadecimal Number System (cont)
• Take the binary number, e.g.:
100110110011111
octal
decimal binary
hex
12
Introduction to Negative Integers
• So far we have concentrated on positive integers
13
Excess Notation
• By convention we count from 0, but what if we count from a different
number. 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 14
= 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
15
Sign Magnitude
• Why not just use the most significant bit to represent the sign?
• This is so simple: 0 = positive, 1 = negative
• In 4 bits: Bin Value Bin Value
0000 0 1000 -0
• There are 2 values for 0! (+0 and -0)
0001 1 1001 -1
• Addition and subtraction complicated 0010 2 1010 -2
16
One’s Complement
• The negative is the complement of the positive
– If positive, do nothing 84 = 0101 0100
– If negative, complement (invert) each bit -84 = 1010 1011
• Negation is easy
– Invert the bit-pattern
17
Two’s Complement
• We want an encoding that:
– Has one encoding for 0 (000)
– Decimal Binary
Binary addition and subtraction works
– Addition “through zero” works -4 100
– Has a sign bit -3 101
– Subtraction is addition of negative numbers -2 110
-1 111
• To convert a positive to two’s complement 0 000
– Do nothing
1 001
• To convert a negative to two’s complement 2 010
– Complement (invert the bits) then add 1
3 011
18
Example
• Convert -84 to two’s complement (using 8 bits)
84 = 0101 0100
11 ← carries
1010 1011 ← each bit complemented
+ 1
---------
1010 1100 ← -84 in two’s complement
19
Two’s Complement to Decimal
• If the high (sign) bit is 0
– Convert the binary number to decimal
20
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
0001 0110 16 + 4 + 2 = 22
= -22
21
Subtraction Example
• Subtract 5 from 18 in 8-bit, 2's complement
18 – 5 = 18 + -5
18 = 0001 0010
-5 = 1111 1011
----------
10000 1101
22
Two’s Complement Example 2
• Subtract 5 from -18 in 8-bit, 2's complement
-18 - 5 = -18 + -5
23
Real Numbers
• Integers are discrete. Real numbers are continuous.
• In Computer Science real numbers are often called
floating point numbers
– For reasons that will become obvious
24
Scientific Notation (decimal)
• If you move the decimal point to the left: 12,345.6 = 0.012345 =
– You increase the exponent. 12,345.6 x 100 0.012345 x 100
• If you move the decimal point to the right: 1,234.56 x 101 0.12345 x 10-1
– You decrease the exponent.
123.456 x 102 1.2345 x 10-2
1 12.3456 x 103 12.345 x 10-3
Remember: 10 1
0
10
p
10 1.23456 x 104 123.45 x 10-4
p
2 1.10001 x 24 25
Floating Point
• In scientific notation the number is broken into 3 parts
– The significant digits
– The power
– The base
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)
• Without the leading 1 before the binary point
– Why?
27
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
28
Decimal to IEEE Conversion Example
• Convert 24.510 to IEEE floating point representation:
= 1.10001 * 24
29
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
30
Decimal to IEEE Conversion Example (cont.)
5. Add 127 to the exponent (from step 2) to get e
e = 4 + 127 = 131
= 1000 0011
31
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
32
IEEE to Decimal Conversion
• Convert C1C4000016 to decimal form
s e f
33
IEEE to Decimal Conversion (cont.)
34
IEEE to Decimal Conversion (cont.)
= 1.10001 x 24
35
IEEE to Decimal Conversion (cont.)
36
Floating Point Arithmetic (+/-)
• Addition (subtraction)
– Align the significands
• Convert to the same 2e (align)
– Add (or subtract)
– Round and normalize
• Convert back into scientific notation
Example: 48 + 2.5
48 = 1.100*25 2.5 = 1.01 * 21
Convert to the same 2e then add
48 = 1 . 1 0 0 0 0 0 *25
2.5 0 . 0 0 0 1 0 1 *25
=
48 + 2.5 = 1 . 1 0 0 1 0 1 *25 = 50.5 37
Floating Point Arithmetic (* & /)
• Multiplication
– The significands are multiplied
– The exponents are added
– Round and normalize
• Division
– The significands are divided
– The exponents are subtracted
– Round and normalize
38
Summary
• Your computer deals with different types of numbers: binary, decimal, octal
and hexadecimal.