0% found this document useful (0 votes)
44 views

Lecture02-Data Representation 2

This document discusses various methods for representing numeric data in computers, including: 1) Positional notation and binary, octal, decimal, and hexadecimal number systems are introduced to represent positive integers in computers. 2) Negative integers can be represented using excess notation, sign-magnitude representation, one's complement, or two's complement methods. Excess notation and sign-magnitude have limitations. 3) Real numbers may be represented using excess notation, which stores the fractional portion in binary.

Uploaded by

usheelike666
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Lecture02-Data Representation 2

This document discusses various methods for representing numeric data in computers, including: 1) Positional notation and binary, octal, decimal, and hexadecimal number systems are introduced to represent positive integers in computers. 2) Negative integers can be represented using excess notation, sign-magnitude representation, one's complement, or two's complement methods. Excess notation and sign-magnitude have limitations. 3) Real numbers may be represented using excess notation, which stores the fractional portion in binary.

Uploaded by

usheelike666
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

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


• Next Lecture
- Computer Architecture 1 and Assembly language

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
• The base 10 system has 10 symbols (numerals / digits):
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• Each digit in the number represents a power of 10


– Working from the ‘.’
• Left increases +ve powers of 10
• Right decreases –ve powers of 10
• 123.45
= 1*102 + 2*101 + 3*100 + 4*10-1 + 5*10-2
= 100 + 20 + 3 + 4/10 + 5/100

• The general case is:


def.xyzb = d*b2 + e*b1 + f*b0 + x*b-1 + y*b-2 +z*b-3

• Where b is the base (specified as a subscript) 4


Binary Number System
• We can use the computer’s memory (the switchbox) to
represent numbers in binary (base 2).

• What is the value of:


– on on off off on on?
– 1100112
• 1*25 + 1*24 + 0*23 + 0*22 + 1*21 + 1*20
• 32 + 16 +0 +0
+2 +1
• 51
– on on off “point” off on on
– 110.0112
• 1*22 + 1*21 + 0*20 + 0*2-1 + 1*2-2 + 1*2-3
• 4 + 2 + 0 + 0 + 1/4 + 1/8
• 6.375
5
Decimal to Binary Conversion
• Convert 38.687510 to binary (base 2)
– We’ll do the 38 first then the .6875 second

• Repeatedly divide the whole number part by 2


– The remainders are the answer (bottom to top)

LSB = least significant bit


MSB = most significant bit

3810 = 1001102 6
Decimal to Binary Conversion (cont)
• Convert 38.687510 to binary (base 2)

• 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
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

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.

• The base 8 system (octal) uses the symbols:0, 1, 2, 3, 4, 5, 6, 7.


- It takes 3 binary bits to make an octal digit.
- Group the binary digits in groups of 3 from the decimal point (the Least Significant

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

• 4 bits (one digit) is a nybble.


2 nybbles is byte (8 bits).

10
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

12
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

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

• Most inconveniently, 0 (excess n) is not 0 in binary

• There must be a better way

• But, excess notation is used to store real numbers


- Will be shown later in this lecture

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

– Compute the bit pattern for 2 - 3 0011 3 1011 -3


0100 4 1100 -4
• The range of integers is (2n-1-1) to -(2n-1-1) 0101 5 1101 -5
– Where n is the number of bits 0110 6 1110 -6
0111 7 1111 -7
– Which is 1 fewer than Excess notation

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

• Addition and subtraction is easier


– But there are still two values for zero

• Range of integers is (2n-1-1) to -(2n-1-1)


– Where n is the number of bits
– Which is 1 fewer than Excess notation

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

• The range of integers is (2n-1-1) to -(2n-1)

18
Example
• Convert -84 to two’s complement (using 8 bits)

84 = 0101 0100

• Complement and add 1

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

• 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

• This is the same as subtracting the positive number from


2n where n is the number of bits in the representation

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

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


[1] 0000 1101 = 13

22
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

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

• Floats can be very large or very small


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

• How do we represent real numbers in the computer?

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

Scientific Notation (binary)


• If you move the decimal point to the left: 11000.1=
– Same case of decimal.
11000.1 x 20
• If you move the decimal point to the right:
1100.01 x 21
– Same case of decimal.
110.001 x 22
11.0001 x 23
Remember: 20  1 1
2  p
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

• The base is usually implicit (we know it)


• If you store the other two separately then the power
determines the location of the decimal point
– That is, the decimal point floats relative to the significant
digits

• We call these numbers floating point numbers


– There’s a number of formats including the IEEE 754 standard
26
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)
• 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. Convert the decimal number to binary


24.510 = 16 + 8 + 0 + 0 + 0 + 0.5
= 11000.1

2. Write the binary number in scientific notation using


base 2

= 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

100 0100 0000 0000 0000 0000

4. Determine sign bit, s

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

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

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

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

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

34
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

35
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

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.

• There are four techniques to represent negative integers:


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

• IEEE Floating Point Format is used to represent floating point numbers


where they could be very small or big numbers.

You might also like