25
COMP201 Computer Systems Number Representation

COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples Englander Chapter

Embed Size (px)

Citation preview

Page 1: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

COMP201 Computer SystemsNumber Representation

Page 2: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Number Representation

Introduction Number Systems Integer Representations Examples

Englander Chapter 2 and Chapter 4.

Page 3: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Introduction

Data must be converted to Binary before it can be stored in Computer

Page 4: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Introduction

Format used will depend on data: e.g. consider the number 9

9 may be represented as 1001 -9 may be represented as 10111 9.0 may be represented as 0000010010000001 the character 9 may be represented as 0111001

And the intended purpose of the number within the computer

Page 5: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Considerations

When a coding format is being devised a number of considerations need to be made: Ease of manipulation Conciseness and machine efficiency Accuracy sufficient for problem Standardised data communication

Page 6: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Number Representation

Computers store and Manipulate numbers using the binary (base 2) number system

Base 10 = 510

Roman = V Binary = 1012

Base3 = 123

Page 7: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Binary Representation

Each bit represents a value two times the value of the bit to it’s right.

Dec Bin0 000 = 0x22 + 0x21 +

0x20

1 001 = 0x22 + 0x21 + 1x20

2 010 = 0x22 + 1x21 + 0x20

3 011 = 0x22 + 1x21 + 1x20

4 100 = 1x22 + 0x21 + 0x20

Page 8: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Octal Representation

Each octal number can be perfectly represented by 3 binary digits

0 0001 0012 0103 0114 1005 1016 1107 111

010 110 101 001

= 26518

=??10

Page 9: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Hexadecimal Representation

Each Hex number can be perfectly represented by 4 binary digits

Two Hex digits can be used to represent a byte; four for a word.

0 00001 00012 00103 00114 01005 01016 01107 01118 10009 1001A 1010B 1011C 1100D 1101E 1110F 1111

0010 1110 1010 0001

= 2EA116

Page 10: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Number Conversion methods

(You should have already studied this in a previous class. If you did not, or don’t remember, study Chapter 2 in detail.)

Base= number of characters in system

Decimal Octal HexadecimalBinary

10 8 16 2

Page 11: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Number Conversion methods

Place value: the value of a digit depends upon its placement relative to a reference, say a decimal point.

For instance, 2610 = 20 + 6

= 2 x 101 + 6 x 100

This leads to methods for converting between bases.

26518= 2 x 83 + 6 x 82 + 5 x 81+ 1 x 80 Substitute decimal values for powers of 8 to convert

26518 into decimal equivalent (1456)

Page 12: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Integer Representations

BCD (binary coded decimal) Sign and Magnitude Excess Notation Two’s complement

Page 13: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

BCD (Binary Coded Decimal) Each Decimal digit is coded as a 4 bit

binary code Developed for early calculators

e.g. 35910 = 0011 0101 1001bcd

Easy for people to understand, Hard for computers to work with

Signed Magnitude Extra bit added to the code to represent the

sign In most cases

a 0 represents a +ve a 1 represents a –ve

BCDvalue

Digit

0000 00001 10010 20011 30100 40101 50110 60111 71000 81001 91010 -1111

notused

Page 14: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

BCD Example

Each Decimal digit is coded as a 4 bit binary code410= 00000100

And two digits, 4510, would be stored in two different memory locations!

00000100 00000101

BCDvalue

Digit

0000 00001 10010 20011 30100 40101 50110 60111 71000 81001 91010 -1111

notused

Page 15: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Packed BCD

Packed BCD simply makes use of the storage space normally wasted in storage of BCD, by using the leftmost 4-bits (nibble) for one digit, and the rightmost nibble for another. 4510= 01000101 in packed BCD

BCD used in some old financial software and calculators, but now very rare.

Page 16: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Sign

Negative integers are often required Computers do not have internal minus signs There are several ways to represent negative

and positive integers Choice is often based on the ease of

manipulation for the intended purpose

Page 17: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Sign and Magnitude

One of the simplest systems is to allocate one bit as the “sign” bit. The other bits are the “magnitude”. E.g. +24 = 00011000

-24 = 10011000 Simple to find the sign Two values for zero Must process sign separately

Page 18: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Excess Notation fix number of bit positions used smallest number with 1 in the

MSB represents zero (e.g. 1000) All bit strings greater than this

represent +ve numbers All bit strings less than this

represent -ve numbers Example is known as excess

eight notation because 8 (1000) represents zero

Bit String Value0000 -80001 -70010 -60011 -50100 -40101 -30110 -20111 -11000 01001 11010 21011 31100 41101 51110 61111 7

Page 19: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Ones Complement

Negative numbers formed by inverting the positive representation (logical NOT) E.g. +24 = 00011000

-24 = 11100111 Two values for zero Addition requires carry bit to be wrapped

around

Page 20: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Two’s Complement

To negate a number invert the bits (logical NOT) then add one. E.g. +24 = 00011000

invert 11100111

Add one -24 = 11100111 + 1

-24 = 11101000

Works for converting negative to positive as well

Page 21: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Two’s Complement

Can represent numbers in the range -2n-1 to (2n-1-1)

Only one representation of zero

Binary

Result

Positive

Negative

Compliment 0 1 1 0

+1

Page 22: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Two’s Complement– another viewpoint: The number wheel

0000

0111

0011

1011

11111110

1101

1100

1010

1001

1000

0110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-8

-7

-6

-5

-4

-3

-2

-1

(For 4 bits) Reference: Katz: Contemporary Logic Design, p243

Page 23: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Two’s Complement

Leftmost bit is always the sign bit All zeroes is the only representation of zero Simple to implement negation, addition and

subtraction in hardware. Can be sign extended

+510 = 00000101 = 0000000000000101 -510 = 11111011 = 1111111111111011

Page 24: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Two’s Complement Examples

Express the following in 2’s complement notation (use 16 bit form): 10000 100111100001001 0100111000100100

Add the following 2’s complement numbers (they are 12 bits):

011001101101 111010111011

Page 25: COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter

Format Use

Ones complement common in older computer hardware

Two’s complement most common signed integer representation today.

Sign and magnitude used in some very early computers

Sign and magnitude and Excess notation both used in common floating point formats