14
CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Embed Size (px)

Citation preview

Page 1: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

CSE 111

Representing Numeric Data in a Computer

Slides adapted from Dr. Kris Schindler

Page 2: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Unsigned Binary Numbers

Range: 02n-1where n is the number of bits

Positional Notation

Example: 101100two

11

22

33

22

11

00 222222

nn

nn bbbbbb

11

223210 228421

nn

nn bbbbbb

44328432116081412010

n -1

012345n -2n -1

n -2n -1

n -2 5 4

4

3 2

2

1

1

bbbbbbbb

0

2 2 3 2 1 6 8W e ig h tB it

P o s itio n

Page 3: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Unsigned Binary Numbers

How do we convert from a decimal number to a binary number?

Continue until q=0

012

22221

11110

0000

...

Re

......

,2/

,2/

,2/

bbbbNumberBinary

mainderr

Quotientq

kBitBinaryb

NumberDecimali

where

rbrqq

rbrqq

rbrqi

n

x

k

Page 4: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Unsigned Binary Numbers

How do we convert from a decimal number to a binary number?Example: 39ten

twoten

b

b

b

b

b

b

10011139

15

04

0

1

1

1

1,02/1

0,12/2

0,22/4

1,42/9

1,92/19

1,192/39

3

2

1

0

Page 5: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Bit Positions

MSBMost Significant BitLeftmost Bit Position

LSB Least Significant BitRightmost Bit Position

Page 6: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

The most significant bit (leftmost) represents the signNegative (-): 1Positive (+): 0

Page 7: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Computers represent signed numbers using two’s complement notation

Page 8: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Two’s ComplementRepresentation of a negative binary number

Consider an n-bit number, x The two’s complement of the number is 2n - xThis process is called taking the two’s complement of a numberTaking the two’s complement of a number negates it

Page 9: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Two’s ComplementShortcut for taking the two’s complement of a number

Start at the least significant (rightmost) bit and move left (toward the most significant bit)

Keep every bit until you reach the first 1Keep that 1Invert every bit (01,1 0) after the first 1 as you continue to

move left

Page 10: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Two’s ComplementExamples:

-4 Take the two’s complement of 4 (00000100) 11111100 = -4

-9 Take the two’s complement of 9 (00001001) 11110111 = -9

Since the above are negative, taking the two’s complement will allow you to determine the magnitude, which is the positive equivalent

Page 11: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Two’s ComplementExamples:

+6 Since the number is positive, you don’t need to take the two’s

complement 000000110 = +6

+18 Since the number is positive, you don’t need to take the two’s

complement 000010010 = +18

Page 12: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Signed Binary Numbers

Two’s ComplementSince taking the two’s complement of a number negates it,

taking the two’s complement twice gives you the original number back

Example:+12 is represented by 00001100Taking the two’s complement results in -12 (11110100)Taking the two's complement of -12 results in +12 (00001100)

Page 13: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

Floating Point

Very large/small numbersFractions

Example8.5 x 223

100.12 x 223

Normalized1.0012 x 227

ExponentBias = 127127+26 = 153 = 100110012

Significand: 00100000000000000000000Sign: 0Number: 01001000100100000000000000000000

S1B its

E x p o n e n t (E )

(-1) X 1.F X 2S (E-bias)

8S ig n if ic an d (F )

2 3

Page 14: CSE 111 Representing Numeric Data in a Computer Slides adapted from Dr. Kris Schindler

References

J. Glenn Brookshear, Computer Science - An Overview, 11th edition, Addison-Wesley as an imprint of Pearson, 2012

Donald D. Givone, Digital Principles and Design, McGraw-Hill, 2003

John L. Hennessy and David A. Patterson, Computer Organization and Design, The Hardware/Software Interface, 3rd Edition, Morgan Kaufmann Publishers, Inc., 2005