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

advertisement

CSE 111

Representing Numeric Data in a

Computer

Slides adapted from Dr. Kris

Schindler

Unsigned Binary Numbers

Range: 0

2 n -1

 where n is the number of bits

Positional Notation

P n n 5 0

3 8 b b b

0

Example: 101100 two

0

2

0

1

2

1

2

2

2

3

2

3 b b

1

0 1

4

2

8

3

1 8 44

Unsigned Binary Numbers

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

q i q

0

1

/

/

/

2

2

 q

0

, q

1

, r

0 r

1

2

 q

2

, r

2

...

i where

 b k r q

Decimal

 x

Binary

Re

Number

Quotient

Bit mainder k b b b

1

0

2

...

 r

0 r

1 r

2

Binary Number

 b n

...

b

2 b

1 b

0

Continue until q =0

Unsigned Binary Numbers

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

Example: 39 ten

39 / 2

19

19

9 /

/ 2

2

9

4 , 1

, 1

, 1

1

4 / 2

2 / 2

/ 2

2 ,

1 , 0

0

0 , 1 b b b b b b

2

1

0

3

1

1

1

0

5

4

1

0

39 ten

100111 two

Bit Positions

MSB

Most Significant Bit

Leftmost Bit Position

LSB

Least Significant Bit

Rightmost Bit Position

Signed Binary Numbers

The most significant bit (leftmost) represents the sign

Negative (-): 1

Positive (+): 0

Signed Binary Numbers

 Computers represent signed numbers using two’s complement notation

Signed Binary Numbers

 Two’s Complement

Representation of a negative binary number

 Consider an n -bit number, x

 The two’s complement of the number is 2 n x

 This process is called taking the two’s complement of a number

 Taking the two’s complement of a number negates it

Signed Binary Numbers

 Two’s Complement

 Shortcut 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 1

 Keep that 1

Invert every bit (0

1,1

0) after the first 1 as you continue to move left

Signed Binary Numbers

 Two’s Complement

Examples:

 4

 9

 Take the two’s complement of 4 (00000100)

1 1111100 = 4

 Take the two’s complement of 9 (00001001)

1 1110111 = 9

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

Signed Binary Numbers

 Two’s Complement

Examples:

 + 6

 Since the number is positive, you don’t need to take the two’s complement

0 00000110 = + 6

+ 18

 Since the number is positive, you don’t need to take the two’s complement

0 00010010 = + 18

Signed Binary Numbers

 Two’s Complement

 Since 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 00001100

 Taking the two’s complement results in -12 (11110100)

Taking the two's complement of -12 results in +12 (00001100)

Floating Point

Very large/small numbers

Fractions

Example

1

E o t )

8

8.5 x 2 23

-

S

100.1

2 x 2 23

Normalized

1.

001

2 x 2 27

Exponent

Bias = 127

 127+26 = 153 = 10011001

2

Significand: 00100000000000000000000

Sign: 0

 Number: 0 10010001 00100000000000000000000 i n i n (

References

 J. Glenn Brookshear, Computer Science - An

Overview , 11 th 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 , 3 rd Edition,

Morgan Kaufmann Publishers, Inc., 2005

Download