Lecture02-Data Representation 2

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 38

CST1500

Computer Systems Architecture and


Operating Systems

Lecture 2 - Data Representation 2

1
Overview
• Last Lecture
– Data representation 1
• This Lecture
– Data representation 2
• Positional notation
• Positive integers
• Negative integers
• Real numbers

– Source: lecture & Chapter 9 of 10th edition

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)

• In a positional system the base is the number of numerals


• Base 10 (decimal) is used today by most cultures.
– Its easy because we have 10 fingers

• Computers work in base 2 (binary)

3
Base 10 Numbers

4
Binary Number System

5
Decimal to Binary Conversion

LSB = least significant bit


MSB = most significant bit

6
Decimal to Binary Conversion (cont)

•For the fractional part, multiply by 2. Split the result into an


integer part and a fractional part. Continue multiplying the
fractional part. Use the integer part from top to bottom.

.687510 = .10112

•So, 38.6875 = 100110.10112


7
Binary to Decimal Conversion (cont)

Binary to Decimal Conversion Table

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

• 4 bits (one digit) is a nybble.


2 nybbles is byte (8 bits).

11
Hexadecimal Number System (cont)
• Take the binary number, e.g.:
100110110011111

• Group into chunks of 4 binary digits (right to left):


[0]100 1101 1001 1111 Bin Hex Bin Hex
0000 0 1000 8
• Convert each group into its nybble: 0001 1 1001 9
0010 2 1010 A
4D9F
0011 3 1011 B
• The resulting number is the hex: 0100 4 1100 C
0101 5 1101 D
1001101100111112 → 4D9F16
0110 6 1110 E
0111 7 1111 25 F
Other Conversions
• Conversions:

- decimal→octal, decimal→hex, octal→decimal, and hex→decimal can


be made using positional notation or similar techniques as
binary→decimal and decimal→binary.

octal

decimal binary

hex

13
Introduction to Negative Integers
• So far we have concentrated on positive integers

• Remember that the memory is a set of switches, there is no


decimal point and no negative sign, but we can devise a
scheme to represent negative numbers

• 4 ways have been used:


– Excess notation
– Sign magnitude
– One's complement
– Two's complement

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

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
0011 3 1011 -3
0100 4 1100 -4
0101 5 1101 -5
0110 6 1110 -6
0111 7 1111 -7

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

• Addition and subtraction is easier


–But there are still two values for zero

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

• If the high (sign) bit is 1


– Complement (invert) each bit
– Add 1 to the binary number
– Convert the binary number to decimal
– Put a minus sign in front of the decimal number

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

Dropping the high-order (9th) bit gives:


[1] 0000 1101 = 13

23
Two’s Complement Example 2
• Subtract 5 from -18 in 8-bit, 2's complement
-18 - 5 = -18 + -5

-18 = 1110 1110


-5 = 1111 1011
----------
11110 1001

Ignoring the high-order (9th) bit gives:

[1] 1110 1001 = -23

24
Real Numbers
• In Computer Science real numbers are often called floating
point numbers

• Floats can be very large or very small


– 4,386,593,021,854.341
– 0.000,000,000,008

• Scientific notation (also referred to as scientific form or standard


index form, or standard form in the UK) is a way of expressing
numbers that are too big or too small to be conveniently written in
decimal form.

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

1 12.3456 x 103 12.345 x 10-3


10 1.23456 x 104 123.45 x 10-4
Scientific Notation (binary) p

• If you move the decimal point to the left:


– Same case of decimal.
11000.1=
• If you move the decimal point to the right:
11000.1 x 20
– Same case of decimal.
1100.01 x 21

Remember: 20  1 110.001 x 22
1 11.0001 x 23
2  p
p

2 1.10001 x 24 26
Floating Point

How do we represent real numbers in the computer?

• Representing floating point numbers


–There’s a number of formats including the IEEE 754 standard

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).

IEEE Standard 754 floating point is the most common


representation today for real numbers on computers, including
Intel-based PC’s, Macs, and most Unix platforms.
IEEE Floating Point Format
• Stored as a binary32 (single) 4-byte number (float)
1-bit 8-bits 23-bits
s e f

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:

1. Convert the decimal number to binary


24.510 = 11000.1

2. Write the binary number in scientific notation using


base 2

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

100 0100 0000 0000 0000 0000

4. Determine sign bit, s

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?

6. Convert e to an 8 bit binary number (add leading


zeroes if needed)
131= 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1

= 1000 0011

7. Write in IEEE format by concatenating s, e, and f

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

C1C4000016 = 1100 0001 1100 0100 0000 0000 0000 0000

1. Group the binary digits into 1, 8, and 23 digits (s,e,f)

1100 0001 1100 0100 0000 0000 0000 0000

s e f

35
IEEE to Decimal Conversion (cont.)

2. Convert e to a decimal number.


1000 0011 = 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1
= 131

•Subtract 127 to get exp

131 – 127 = 4 (exponent)

36
IEEE to Decimal Conversion (cont.)

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

100 0100 0000 0000 0000 0000


 10001

= 1.10001 x 24

4. Un-normalise the number by moving the binary point


until the exp = 0
1.10001 x 24 = 11000.1

37
IEEE to Decimal Conversion (cont.)

5. Convert the binary number to decimal


11000.1 = 16 + 8 + 0 + 0 + 0 + 0.5
= 24.5

6. If s is 1, negate the number


s = 1, so the number is negative

The answer is -24.5

38

You might also like