67
DATA REPRESENTATION Y. Colette Lemard February 2009 2

DATA REPRESENTATION Y. Colette Lemard February 2009 2

Embed Size (px)

Citation preview

DATA

REPRESENTATION

Y. Colette Lemard

February 2009

2

2

TYPES OF NUMBER

REPRESENTATION

3

There are two major classes of number representation :-

FIXED POINT

FLOATING POINT

4

FIXED POINT

SYSTEMS

5

Most of us are familiar with fixed representation to some extent

Let us review it briefly – Binary coded decimal

Signed magnitude (sign & magnitude)

One’s complement

Two’s complement

6

BINARY

CODED DECIMAL

7

B C DIn BCD

representation, we use 4 bits to represent the denary numbers from 1 to 9.

In addition we use 1010 for a positive sign (optional) and 1011 for a negative sign

BCD BCD

0 0000 5 0101

1 0001 6 0110

2 0010 7 0111

3 0011 8 1000

4 0100 9 1001

8

What is the BCD of 947?

1010100101000111

What is the BCD of –836?

1011100000110110

9

What is the denary of the following BCD numbers?

1011011101000101 ?

-745

10100110000000110010 ?

6032

10

SIGNED

MAGNITUDE

11

SIGN AND MAGNITUDE

S&M, as the name suggests, shows both the sign and the size of a number in (usually) one byte. The first bit is usually reserved for the sign and the rest of the bits show the size (or magnitude) of the number.

sign bit : 0 for positive, 1 for negative

12

When converting to S&M binary one would therefore give the binary figure to 7 bits then place the sign bit in front (to the left) of the number.

00000110 6

10000110 -6

01111111 127

11111111 -127

00000000 0

10000000 0

13

When converting S&M binary back to denary one would firstly remove the sign bit then convert the remaining 7 bits. An examination of the sign bit would then determine if the figure is positive or negative.

The range of S&M numbers which can be stored in 8 bits is

-127 to 127

14

ONE’S

COMPLEMENT

15

One’s complement is a binary concept i.e. a number first has to be in binary before we can find its one’s complement.

To find the one’s complement of a number we flip all the bits in the number i.e. change 1’s to 0’s and 0’s to 1’s.

This is called NOTting the number; the NOT of 1 is 0 and the NOT of 0 is 1

16

The one’s complement of a number is a form of the negative of the number.

NumberOne’s Complement

0000 0 1111 0

0001 1 1110 -1

0010 2 1101 -2

0011 3 1100 -3

0100 4 1011 -4

17

Note that both in Sign and

Magnitude and One’s Complement

the number zero has two

representations.

This is inconvenient as making a

comparison for zero then becomes

cumbersome

The Two’s Complement system

addresses this shortcoming

18

TWO’S

COMPLEMENT

19

Most computers

actually use two’s

complement when

manipulating numbers.

20

When you find the two’s complement

of a number you are finding the

negative of that number.

The two’s complement representation

of a positive number is the same as

the straight binary of that number.

21

To find the two’s complement of a

number we first find its one’s

complement then we add 1 to that

number.

Remember to ensure that you are

working in one byte unless told

otherwise.

We ignore/discard any overflows

22

Finding the Two’s Complement of a Denary Number

Find the two’s complement of 72

72 is 1001000

i.e. 01001000 in 8 bits

Flipping the bits gives 10110111

Adding 1 gives 10111000

23

Finding the Two’s Complement of a Denary Number

Find the two’s complement of 31

31 is 11111

i.e. 00011111 in 8 bits

Flipping the bits gives 11100000

Adding 1 gives 11100001

24

Finding the Two’s Complement of a Denary Number

Find the two’s complement of

47 63

81 25

11010001 11000001

10101111 11100111

25

When we look at a number said to be two’s complement we can immediately tell if it is negative or positive by looking at the left most bit

Is this familiar?

26

Converting Two’s Complement to Denary

Look at the leftmost bit

If it is a 1 then use the two’s complement method

If it is a 0 convert as for straight binary

27

Converting to denary from two’s complement

Flip the bits then add 1

28

What denary numbers are these?

11011011 11110010

01101001 10011100

-37 -14

105 -100

29

Because we use the two’s complement system to find the negative of numbers, we can use it to do binary subtraction

This is in fact one of the most useful aspects of two’s complement

30

We firstly need to recall a basic math rule

A – B = > A + ( -B )

This means that we can always substitute an addition for subtraction as long as we can find the negative of the number being subtracted.

31

In binary we can find the negative of a number by using its two’s complement

ThereforeBinaryA – BinaryB

BinaryA

+

(two’s complement of BinaryB)

32

Work in binary and find 7 - 2

7 111 00000111

2 10 00000010 - 2 11111101 + 1 11111110

So 7 - 2 00000111 + 11111110 100000101

Since we are working in 4 bits we discard the leftmost bit so the answer is 000001012

33

Work in binary and find 23 - 11

23 10111 00010111

11 1011 00001011

- 11 11110100 + 1 11110101

(Here I choose to work in 8 bits because 23 is too long for 4 bits)

34

Work in binary and find 23 - 11

So 23 - 11

00010111 + 11110101100001100

Since we are working in 8 bits we discard the leftmost bit so the answer is 000011002

35

Perform the following using two’s complement arithmetic

1. 69 – 26

2. 14 – 5

3. 100 – 50

4. 77 – 33

5. 81 - 81

36

Fixed Point ?

We have been looking exclusively at integers.

An integer can be said to be a number with its decimal point right at the end after the most rightmost digit and with no decimal places after the point.

37

FIXED POINT REPRESENTATION

Integers are an example of fixed point numbers

Fixed Point is so called because the decimal point is steady with there always being the same number of places before and the same number of places after it.

38

Let’s suppose I have 8 bits in which to represent a number in the computer. Remember I can only use 1 and 0.

How do I represent a decimal point?

There is no symbol for the decimal point in binary

39

What I could do however is determine before hand that all numbers are allowed to have 2 decimal places and let the computer system know that this is a general rule.

My byte would be :-

40

I would then have fixed my decimal point to be always after the 2nd position reading from the right. This leaves 6 positions for the whole number part of any value

This seems adequate enough for currency values

But what about very small values measured in science?

41

Maybe I need to use 5 values for the decimal place.

But that would leave only 3 for the whole number.

Seems whatever I do I’m compromising on how large or how small a number my system can store.

42

11111 i.e. 31

Let’s assume that we are working with 8 bits and the decimal point is after the first 5 bits.

What’s the largest integer value I can store if I am using straight binary?

43

What’s the largest number overall?

11111.1112

But what is this in denary?

To answer that we need to understand binary fractions

44

BINARY FRACTIONS

Let us agree on the following about decimals in the denary system

.npxyz

n/101 + p/102 + x/103 + y/104 + z/105 …

n/10 + p/100 + x/1000 + y/10000 + z/100000 …

45

BINARY FRACTIONS

In the binary system therefore

.npxyz

n/21 + p/22 + x/23 + y/24 + z/25 …

n/2 + p/4 + x/8 + y/16 + z/32 …

46

BINARY FRACTIONS

Converting binary fractions to denary

.01001

0/21 + 1/22 + 0/23 + 0/24 + 1/25

1/4 + 1/32 => .28125

47

BINARY FRACTIONS

In the binary system therefore

.11001

1/21 + 1/22 + 0/23 + 0/24 + 1/25

1/2 + 1/4 + 1/32 => .78125.

48

BINARY FRACTIONS

In the binary system therefore

.010111

0/21 + 1/22 + 0/23 + 1/24 + 1/25 + 1/26

1/4 + 1/16 + 1/32 + 1/64 => .359375

49

.1 ½ .05

.01 ¼ .025

.001 1/8 .125

.0001 1/16 .0625

.00001 1/32 .03125

.000001 1/64 .015625

Binary Fractions Table

50

Let us return to our original number 11111.111

We already know that the integer part is 31

The fractional part is => .875

Therefore 11111.1112 = 31.87510

51

Assuming that the decimal point is fixed after the fourth position convert the following binary numbers to denary

1. 00011000 2. 00100100

1.5 2.25

3. 10101010 4. 01111110

10.625 7.875

5. 10011001 6. 11110000

9.5625 15.0

52

What is the largest number and the smallest number that can be held in two bytes assuming that the decimal point is fixed in the centre.

Largest11111111.11111111

510.9960938

Smallest 00000000.00000001

0.0039063

53

Suppose you have a denary fraction and want to know its binary representation?

Us the multiply by 2 with integerr method

(makes sense doesn’t it?)

54

Convert .07510 to binary

.75 X 2 = 1.5

.5 X 2 = 1.0

We stop when we are about ot multiply by zero

Taking the integers reading from the top down gives .112

55

Convert .103610 to binary

.1036 X 2 = 0.2072 (take off the 0)

.2072 X 2 = 0.4144 (take off the 0)

.4144 X 2 = 0.8288 (take off the 0)

.8288 X 2 = 1.6576 (take off the 1)

.6576 X 2 = 1.3152 (take off the 1)

We can continue but lets stop at 5 significant digits

Taking the integers reading from the top down gives .000112

56

Convert .062510 to binary

.0625 X 2 = 0.125 (take off the 0)

.125 X 2 = 0.25 (take off the 0)

.25 X 2 = 0.5 (take off the 0)

.5 X 2 = 1.0 (take off the 1)

We stop here because the next line would see us multiplying by zero

Taking the integers reading from the top down gives .00012

57

Convert to binary

1. .0865

2. .146

3. .149

4. .652

5. .1725

6. .365

58

What is the largest number and the smallest number that can be held in two bytes assuming that the decimal point is fixed with 5 bits following the decimal point.

Largest11111111111.11111

4094.96876

Smallest 00000000000.00001

0.03125

59

What is the largest number and the smallest number that can be held in two bytes assuming that the decimal point is fixed with 2 bits following the decimal point.

Largest11111111111111.11

131070.75

Smallest 00000000000000.01

0.25

60

One therefore compromises on the whole number in order to get more precision on the fraction or can accommodate only a limited number of fractions in order to get a larger whole number.

61

Though we can store fractions using fixed point representation the range of numbers is very limited. Even using 32 bits (one word on a modern pc) and using 8 bits for the fraction we can hold a maximum of just over 8 million

62

101.01 is 5.25

101.10 is 5.5

101.11 is 5.75

We can not get in between those values so it is impossible to

accurately represent 5.4 or 5.65

63

For denary fractions we get down to 1/10th of a value but with binary fractions only .5, or .5 of .5, or .5 of .5 of .5.

Trying to map an infinite number of values onto a finite number of bit patterns results in loss of precision because rounding off is needed.

64

Advantages of Fixed Point Notation

Simple arithmetic

Faster processing

65

Disadvantages of Fixed Point Notation

Loss of precision

Limited range of numbers can be represented

66

Questions ?

67

~ The E N D ~