chapter 2staff.cs.psu.ac.th/sathit/comor/ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 decimal to...

119
Data Representation in Computer Systems Chapter 2

Upload: dothu

Post on 30-Aug-2018

248 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Data Representation in

Computer Systems

Chapter 2

Page 2: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

2

Chapter 2 Objectives

• เพอใหเขาใจการแทนตวเลข และการจดการตวเลขใน digital

Computer

• มทกษะในการเปลยนระหวางระบบเลขฐาน (radix system)

• เขาใจ ขอผดพลาดทเกดขน

– overflow

– Truncation / underflow

Page 3: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

3

Chapter 2 Objectives

• เขาใจความคดพนฐานการแทนเลขทศนยม (floating point)

• การใชรหสตวอกขระทนยม

• เขาใจความคดเกยวกบการตรวจจบขอผดพลาด และการแกไขรหส

Page 4: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

• A bit is the most basic unit of information in a

computer.

• A byte is a group of eight bits.

• A word is a contiguous group of bytes.

• A group of four bits is called a nibble (or nybble). : 4 บตสง 4 บตต า

4

2.1 Introduction

Page 5: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

5

2.2 Positional Numbering Systems

• The decimal number 947 in powers of 10 is:

• The decimal number 5836.47 in powers of 10 is:

5 10 3 + 8 10 2 + 3 10 1 + 6 10 0

+ 4 10 -1 + 7 10 -2

9 10 2 + 4 10 1 + 7 10 0

Page 6: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

6

2.2 Positional Numbering Systems

• The binary number 11001 in powers of 2 is:

• When the radix of a number is something other

than 10, the base is denoted by a subscript.

– Sometimes, the subscript 10 is added for emphasis:

110012 = 2510

1 2 4 + 1 2 3 + 0 2 2 + 0 2 1 + 1 2 0

= 16 + 8 + 0 + 0 + 1 = 25

Page 7: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

7

2.3 Decimal to Binary Conversions

• ในการเปลยนฐานเลข สามารถท าได 2 แบบ

–การลบ (subtraction)

–การหา เอาเศษ

(ไมนยมใช การลบ)

Page 8: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

8

• Suppose we want to convert

the decimal number 190 to

base 3.

– We know that 3 5 = 243 so

our result will be less than six

digits wide. The largest

power of 3 that we need is

therefore 3 4 = 81, and

81 2 = 162.

– Write down the 2 and subtract

162 from 190, giving 28.

2.3 Decimal to Binary Conversions

Page 9: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

9

• Converting 190 to base 3...

– The next power of 3 is

3 3 = 27. We’ll need one of

these, so we subtract 27 and

write down the numeral 1 in

our result.

– The next power of 3, 3 2 = 9,

is too large, but we have to

assign a placeholder of zero

and carry down the 1.

2.3 Decimal to Binary Conversions

Page 10: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

10

2.3 Decimal to Binary Conversions

• Converting 190 to base 3...

– 3 1 = 3 is again too large, so

we assign a zero placeholder.

– The last power of 3, 3 0 = 1,

is our last choice, and it gives

us a difference of zero.

– Our result, reading from top

to bottom is:

19010 = 210013

Page 11: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

สรปใชวธการลบ

11

เปลยน190 จากฐาน 10 เปน ฐาน 3 : (190)10 = (???)3 5 4 3 2 1 0

2 1 0 0 1

243 81 27 9 3 1

162 = 81 x 2

28

27 = 27 x 1

1 0 = 9 x 0

0

0 = 3 x 0

0 1 = 1 x 1

1

190

Page 12: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

12

2.3 Decimal to Binary Conversions

• Another method of converting integers from

decimal to some other radix uses division.

• This method is mechanical and easy.

• It employs the idea that successive division by a

base is equivalent to successive subtraction by

powers of the base.

• Let’s use the division remainder method to again

convert 190 in decimal to base 3.

Page 13: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

13

• Converting 190 to base 3...

– First we take the number

that we wish to convert and

divide it by the radix in

which we want to express

our result.

– In this case, 3 divides 190

63 times, with a remainder

of 1.

– Record the quotient and the

remainder.

2.3 Decimal to Binary Conversions

Page 14: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

14

• Converting 190 to base 3...

– 63 is evenly divisible by 3.

– Our remainder is zero, and

the quotient is 21.

2.3 Decimal to Binary Conversions

Page 15: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

15

• Converting 190 to base 3...

– Continue in this way until

the quotient is zero.

– In the final calculation, we

note that 3 divides 2 zero

times with a remainder of 2.

– Our result, reading from

bottom to top is:

19010 = 210013

2.3 Decimal to Binary Conversions

Page 16: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

16

2.3 Decimal to Binary Conversions

การเปลยนเลขฐาน 10 เปนฐาน 2 โดยการหารเอาเศษ

• (37)10 = (100101)2

• (103)10 = (1100111)2

• (317)10 = (100111101)2

Page 17: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

17

2.3 Decimal to Binary Conversions

การเปลยนเลขฐาน 10 เปนฐาน 2 โดยการหารเอาเศษ

• (120)10 = (xxx)2

• (517)10 = (xxx)2

• (1023)10 = (xxx)2

Page 18: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

18

2.3 Decimal to Binary Conversions

การเปลยนเลขฐาน 2 เปนฐาน 10 โดยน า คาต าแหนงนน คณดวยฐานยกก าลงในต าแหนงทอย

• (1001)2 = (9)10

• (110100)2 = (52)10

• (10010010)2 = (146)10

Page 19: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

19

2.3 Decimal to Binary Conversions

การเปลยนเลขฐาน 2 เปนฐาน 10 โดยน า คาต าแหนงนน คณดวยฐานยกก าลงในต าแหนงทอย

• (100101)2 = (x)10

• (110111)2 = (x)10

• (10110110)2 = (x)10

Page 20: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

20

2.3 Decimal to Binary Conversions

• สวนทเปน ทศนยม หรอ เศษสวน ฐาน 10

709.52 = 7x102 + 0x101 + 9x 100

+ 5x10-1 + 2x10-2

• สวนทเปนเศษสวน ฐาน 2 ของ 101.012

101.01 = 1x22 + 0x21 + 1x 20 +

0x2-1 + 1x2-2

Page 21: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

• เปลยนเศษสวน 5.25 ฐาน 10 เปน ฐาน 2

(5.25)10 = (101.01)2

101.01

21

2.3 Decimal to Binary Conversions

.25 x 2 = 0.5

.5 x 2 = 1.0

หยด

Page 22: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

• เปลยน 100.62510 เปน ฐาน 2

(100.625)10 = (1100100.101)2

1100100.101

22

2.3 Decimal to Binary Conversions

.625 x 2 = 1.250

.25 x 2 = 0.5

.5 x 2 = 1.0

หยด

Page 23: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

• เปลยน 17.810 เปน ฐาน 2

(17.8)10 = (10001.11001)2

23

2.3 Decimal to Binary Conversions

.8 x 2 = 1.6 .6 x 2 = 1.2 .2 x 2 = 0.4 .4 x 2 = 0.8 .8 x 2 = 1.6 .6 x 2 = 1.2

หยด ถาตองการ 5ต าแหนง

Page 24: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

24

2.3 Decimal to Binary Conversions

• เปลยนเลขทศนยมฐาน 10 ตอไปนเปน ฐาน 2

(โดยใหมทศนยม 5 ต าแหนง)

51.12510 = (xxx)2

101.910 = (xxx)2

131.562510 = (xxx)2

199.4210 = (xxx)2

Page 25: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

25

2.3 Decimal to Binary Conversions

• แปลง 1011.0112 เปน ฐาน 10

1011.0112 = 11.37510

0 + 1 + 1

2 4 8

0 + 0.25 + 0.125

Page 26: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

26

2.3 Decimal to Binary Conversions

• เปลยนเศษสวน ฐาน 2 เปน ฐาน 10

10.11012 = xxx10

1011.1012 = xxx10

11.11002 = xxx10

Page 27: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

27

2.3 Decimal to Binary Conversions

• เปลยนเลข ฐาน 2 เปน ฐาน 8 (นบ 3 จาก LSB : บตทมนยส าคญนอยสด ทเหลอไมครบ 3 เตม 0 ดานหนา)

(10 100 101 001)2 = (2451)8

010

Page 28: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

28

2.3 Decimal to Binary Conversions

• เปลยนเลข ฐาน 2 เปน ฐาน 16 (นบ 4 จาก LSB : บตทมนยส าคญนอยสด ทเหลอไมครบ 4 เตม 0 ดานหนา)

(101 0010 1001)2 = (529)16

0101

Page 29: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

29

2.3 Decimal to Binary Conversions

• เปลยนจ านวนจรง ฐาน 2 เปน ฐาน 8 (นบ 3)

– หนาจด นบจาก LSB ขาดเตม 0 ดานหนา

– หลงจด นบจาก MSB ขาดเตม 0 ดานหลง

(10011.011)2 = ?8

(010 011.011)2 = (23.3)8

(1111.11)2 = ?8

(001 111.110)2 = (17.6)8

Page 30: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

30

2.3 Decimal to Binary Conversions

• เปลยนจ านวนจรง ฐาน 2 เปน ฐาน 8

(11000110.1)2 = (xxxxx)8

(1.11010)2 = (xxxxx)8

Page 31: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

31

2.3 Decimal to Binary Conversions

• การแปลงเลขจ านวนจรงฐาน 2 เปน ฐาน 16

– หนาจด นบจาก LSB ขาดเตม 0 ดานหนา

– หลงจด นบจาก MSB ขาดเตม 0 ดานหลง

(10011.011)2 = ?16

(0001 0011.0110)2 = (13.6)16

(1111.11)2 = ?16

(1111.1100)2 = (F.C)16

Page 32: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

32

2.3 Decimal to Binary Conversions

• การแปลงเลขจ านวนจรงฐาน 2 เปน ฐาน 16

(11000110.1)2 = (xxxxx)16

(1.11010)2 = (xxxxx)16

Page 33: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

33

2.3 Decimal to Binary Conversions

• เปลยนเศษสวน ฐาน 10 เปน ฐาน 8

• ท างานเหมอนกบเปนเปลยนเศษสวน ฐาน 10 เปน ฐาน 2 คอหารดวยฐานไปเรอยๆ

(12.5)10 = ?8

[(12)/8.(58)]10 = (14.4)8

4.0 181+480

Page 34: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

34

2.3 Decimal to Binary Conversions

• เปลยนเศษสวน ฐาน 10 เปน ฐาน 16

• ท างานเหมอนกบเปนเปลยนเศษสวน ฐาน 10 เปน ฐาน 2 คอหารดวยฐานไปเรอยๆ

(12.5)10 = ?16

[(12/16).(516)]10 = (C.8)16

8.0 C160

Page 35: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

35

2.4 Signed Integer Representation

• The conversions we have so far presented

have involved only positive numbers.

• To represent negative values, computer

systems allocate the high-order bit to indicate

the sign of a value.

– The high-order bit is the leftmost bit in a byte. It

is also called the most significant bit.

• The remaining bits contain the value of the

number.

Page 36: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

36

2.4 Signed Integer Representation

Signed Integer Representation

การแสดงเลขจ านวนเตม แบบมเครองหมาย

Page 37: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

37

2.4 Signed Integer Representation

• There are three ways in which signed binary

numbers may be expressed:

– Signed magnitude,

– One’s complement and

– Two’s complement.

• In an 8-bit word, signed magnitude

representation places the absolute value of

the number in the 7 bits to the right of the

sign bit.

Page 38: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

38

2.4 Signed Integer Representation

การแทนขอมลขนาด 8 บต

LSB MSB

7 6 0

Sign Bit = 1 มคาเปนลบ

= 0 มคาเปนบวก

Sign bit : เครองหมาย Magnitude : ขนาด

Page 39: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

39

2.4 Signed Integer Representation

1 111 1111

:

1 000 0001

1 000 0000

0 000 0000

0 000 0001

:

0 111 1111 -127 … -1 -0 +0 +1 … +127

• Data range

Binary Decimal

0 0001001

1 0110101

1 0100100

0 1111111

+9

-53

-40

+127

• For 8-bit in memory

Page 40: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

40

2.4 Signed Integer Representation

1 111 1111

:

1 000 0000

0 000 0000

0 000 0001

:

0 111 1111

-127 … -1 -0 +0 +1 … +127

Binary Decimal

0 0001001

1 1110110

1 1011000

0 1111111

0 1111111

+9

-9

-40

-127

+127

• For 8-bit in memory for 1’s Complement

• Data range

Page 41: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

41

2.4 Signed Integer Representation

• 2’s Complement

–Invert and +1 1 000 0000

:

1 111 1111

0 000 0000

0 000 0001

:

0 111 1111

-128 … -1 +0 +1 … +127

Binary Decimal

0 0001001

1 1110111

1 1011100

1 0000000

0 1111111

+9

-9

-40

-128

+127

• Data range

Page 42: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

42

2.4 Signed Integer Representation

Page 43: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

43

2.4 Signed Integer Representation

• For example, in 8-bit signed magnitude, positive 3 is: 00000011

• Negative 3 is: 10000011

• Computers perform arithmetic operations on

signed magnitude numbers in much the same

way as humans carry out pencil and paper

arithmetic.

– Humans often ignore the signs of the operands

while performing a calculation, applying the

appropriate sign after the calculation is complete.

Page 44: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

44

2.4 Signed Integer Representation

• Binary addition is as easy as it gets. You need

to know only four rules: 0 + 0 = 0 0 + 1 = 1

1 + 0 = 1 1 + 1 = 10

• The simplicity of this system makes it possible

for digital circuits to carry out arithmetic

operations.

0 + 0 = 00 0 + 1 = 01

1 + 0 = 01 1 + 1 = 10

SUM Carry

Page 45: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

2.4 Signed Integer Representation

• Positive numbers is the same in one’s

complement and two’s complement (as

well as signed-magnitude), the process of

adding two positive binary numbers is the

same.

45

Page 46: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

2.4 Signed Integer Representation

• How to perform addition using two’s

complement notation (and hence

subtraction, because we subtract a number

by adding its opposite).

• Ex: Add 910 to –2310 using two’s

complement arithmetic.

46

Page 47: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Two’s complement arithmetic

operation

• Ex: Find the sum of 2310 and –910 in binary

using two’s complement arithmetic.

47

• Ex: Find the sum of 111010012 (–23) and

111101112 (–9) using two’s complement addition.

Page 48: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Overflow A Simple Rule for Detecting an Overflow

Condition in Signed Numbers:

• If the carry into the sign bit equals the carry out of

the bit, no overflow has occurred.

• If the carry into the sign bit is different from the

carry out of the sign bit, overflow (and thus an

error) has occurred. See Ex.

48

Page 49: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Overflow

Consider 4-bit unsigned and signed numbers.

• If we add the two unsigned values 0111 (7)

and 0001 (1), we get 1000 (8).

– There is no carry (out), and thus no error.

• If we add the two unsigned values 0111 (7)

and 1011 (11), we get 0010 with a carry,

indicating that there is an error (indeed, 7 + 11

is not 2).

49

Page 50: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Overflow in signed numbers

• Adding the two’s complement integers 0101

(+5) and 0011 (+3) equal to 1000 (–8),

which is clearly incorrect.

– The problem is that we have a carry in to the

sign bit, but no carry out, which indicates that

we have an overflow.

• However, add 0111 (+7) and 1011 (–5), we

get the correct result: 0010 (+2).

– We have both a carry in to and a carry out of

the leftmost bit, so there is no error. 50

Page 51: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Overflow summation

• The rule of thumb used to determine when

carry indicates an error depends on

whether we are using signed or unsigned

numbers.

– Unsigned numbers: a carry (out of the leftmost

bit) indicates the total number of bits was not

large enough to hold the resulting value, and

overflow has occurred.

– Signed numbers: if the carry in to the sign bit

and the carry (out of the sign bit) differ, then

overflow has occurred. 51

Page 52: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Examples of Carry and Overflow

in Signed Numbers

52

Page 53: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

2.4.4 Unsigned Versus Signed Numbers

• Unsigned numbers represent values that are guaranteed

not to be negative.

• A computer programmer must be able to manage both

signed and unsigned numbers.

– For instance, the C language has int and unsigned int for

integer variables, defining signed and unsigned integers,

respectively.

– Different type declarations, many languages have different

arithmetic operations with signed and unsigned numbers.

• Good programmers understand this condition and

make appropriate plans to deal with the situation

before it occurs. 53

Page 54: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

• วงจรเกทสรางการบวก บทแรก (LSB)

54

2.4 Signed Integer Representation

A B Sum Carry

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

XOR AND

Page 55: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

2.4.7 Binary Multiplication and

Division Using Shifting

55

Page 56: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

56

2.4 Signed Integer Representation

Page 57: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

signed magnitude

• มบตบอกเครองหมาย

0 : บวก 1 : ลบ

• บตบอกขนาด 57

2.4 Signed Integer Representation

Page 58: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

58

2.4 Signed Integer Representation

• Example:

– Using signed magnitude

binary arithmetic, find the

sum of 75 and 46.

• First, convert 75 and 46 to

binary, and arrange as a sum,

but separate the (positive)

sign bits from the magnitude

bits.

Page 59: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

59

2.4 Signed Integer Representation

• Example:

– Using signed magnitude

binary arithmetic, find the

sum of 75 and 46.

• Just as in decimal arithmetic,

we find the sum starting with

the rightmost bit and work left.

Page 60: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

60

2.4 Signed Integer Representation

• Example:

– Using signed magnitude

binary arithmetic, find the

sum of 75 and 46.

• In the second bit, we have a

carry, so we note it above the

third bit.

Page 61: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

61

2.4 Signed Integer Representation

• Example:

– Using signed magnitude

binary arithmetic, find the

sum of 75 and 46.

• The third and fourth bits also

give us carries.

Page 62: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

62

2.4 Signed Integer Representation

• Example:

– Using signed magnitude binary

arithmetic, find the sum of 75

and 46.

• Once we have worked our way

through all eight bits, we are

done.

In this example, we were careful careful to pick two

values whose sum would fit into seven bits. If that is not

the case, we have a problem.

Page 63: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

63

2.4 Signed Integer Representation

• Example:

– Using signed magnitude binary

arithmetic, find the sum of 107

and 46.

• We see that the carry from the

seventh bit overflows and is

discarded, giving us the

erroneous result: 107 + 46 = 25.

Page 64: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

64

2.4 Signed Integer Representation

• The signs in signed

magnitude representation

work just like the signs in

pencil and paper arithmetic.

– Example: Using signed

magnitude binary arithmetic,

find the sum of - 46 and - 25.

• Because the signs are the same, all we do is

add the numbers and supply the negative sign

when we are done.

Page 65: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

65

2.4 Signed Integer Representation

• Mixed sign addition (or

subtraction) is done the

same way.

– Example: Using signed

magnitude binary arithmetic,

find the sum of 46 and - 25.

• The sign of the result gets the sign of the number

that is larger.

– Note the “borrows” from the second and sixth bits.

Page 66: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

66

2.4 Signed Integer Representation

• Signed magnitude representation is easy for

people to understand, but it requires complicated computer hardware.

• Another disadvantage of signed magnitude is

that it allows two different representations for

zero: positive zero and negative zero.

Page 67: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

67

2.4 Signed Integer Representation

• In complement systems, negative values are

represented by some difference between a number and its base.

• In diminished radix complement systems, a

negative value is given by the difference between

the absolute value of a number and one less than

its base.

• In the binary system, this gives us one’s

complement. It amounts to little more than flipping

the bits of a binary number.

Page 68: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

one’s complement.

•คาลบเกบแบบสลบบต

ตองการเกบในหนวยความจ าขนาด 8 บต (5)10 = (0000 0101)2

(-5)10 = (1111 1010)2

68

2.4 Signed Integer Representation

Page 69: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

69

2.4 Signed Integer Representation

• one’s complement.

• For example, in 8-bit one’s complement, positive 3 is: 00000011

• Negative 3 is: 11111100

• In one’s complement, as with signed

magnitude, negative values are indicated by a

1 in the high order bit.

Page 70: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

70

2.4 Signed Integer Representation

• With one’s complement

addition, the carry bit is

“carried around” and added

to the sum.

– Example: Using one’s

complement binary arithmetic,

find the sum of 48 and - 19

We note that 19 in one’s complement is 00010011,

so -19 in one’s complement is: 11101100.

Page 71: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

71

2.4 Signed Integer Representation

• Although the “end carry around” adds some

complexity, one’s complement is simpler to implement than signed magnitude.

• But it still has the disadvantage of having two

different representations for zero: positive

zero and negative zero.

• Two’s complement solves this problem.

• Two’s complement is the radix complement of

the binary numbering system.

Page 72: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Two’s complement.

•คาลบเกบแบบสลบบต +1

ตองการเกบในหนวยความจ าขนาด 8 บต (5)10 = (0000 0101)2

(-5)10 = (1111 1011)2

72

2.4 Signed Integer Representation

Page 73: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

73

2.4 Signed Integer Representation

• To express a value in two’s complement:

– If the number is positive, just convert it to binary and

you’re done.

– If the number is negative, find the one’s complement of

the number and then add 1.

• Example:

– In 8-bit one’s complement, positive 3 is: 00000011

– Negative 3 in one’s complement is: 11111100

– Adding 1 gives us -3 in two’s complement form: 11111101.

Page 74: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

74

2.4 Signed Integer Representation

• With two’s complement arithmetic, all we do is add

our two binary numbers. Just discard any carries

emitting from the high order bit.

We note that 19 in one’s complement is: 00010011,

so -19 in one’s complement is: 11101100,

and -19 in two’s complement is: 11101101.

– Example: Using one’s

complement binary

arithmetic, find the sum of

48 and - 19.

Page 75: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

75

2.4 Signed Integer Representation

• When we use any finite number of bits to

represent a number, we always run the risk of

the result of our calculations becoming too large

to be stored in the computer.

• While we can’t always prevent overflow, we can

always detect overflow.

• In complement arithmetic, an overflow condition

is easy to detect.

Page 76: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

76

2.4 Signed Integer Representation

• Example:

– Using two’s complement binary

arithmetic, find the sum of 107

and 46.

• We see that the nonzero carry

from the seventh bit overflows into

the sign bit, giving us the

erroneous result: 107 + 46 = -103.

Rule for detecting signed two’s complement overflow: When

the “carry in” and the “carry out” of the sign bit differ,

overflow has occurred.

Page 77: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

77

2.4 Signed Integer Representation

0110

Page 78: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

78

2.4 Signed Integer Representation

• Signed and unsigned numbers are both useful.

– For example, memory addresses are always unsigned.

• Using the same number of bits, unsigned integers

can express twice as many values as signed

numbers.

• Trouble arises if an unsigned value “wraps around.”

– In four bits: 1111 + 1 = 0000.

• Good programmers stay alert for this kind of

problem.

Page 79: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

79

2.4 Signed Integer Representation

Page 80: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

80

2.4 Signed Integer Representation

Page 81: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

81

2.4 Signed Integer Representation

Page 82: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

82

2.4 Signed Integer Representation

Page 83: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

83

2.4 Signed Integer Representation

Page 84: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

84

2.4 Signed Integer Representation

Page 85: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

85

2.4 Signed Integer Representation

Page 86: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

86

2.4 Signed Integer Representation

Page 87: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

87

2.4 Signed Integer Representation

Page 88: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

88

2.4 Signed Integer Representation

Page 89: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

89

2.5 Floating-Point Representation

Floating-Point Representation

การแสดง Floating-Point

Page 90: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

90

2.5 Floating-Point Representation

• Floating-point numbers allow an arbitrary

number of decimal places to the right of the

decimal point.

– For example: 0.5 0.25 = 0.125

• They are often expressed in scientific notation.

– For example:

0.125 = 1.25 10-1

5,000,000 = 5.0 106

Page 91: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

91

2.5 Floating-Point Representation

• Computers use a form of scientific notation for

floating-point representation

• Numbers written in scientific notation have three

components:

Page 92: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

92

• Computer representation of a floating-point

number consists of three fixed-size fields:

• This is the standard arrangement of these fields.

2.5 Floating-Point Representation

Page 93: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

93

• The one-bit sign field is the sign of the stored value.

• The size of the exponent field, determines the

range of values that can be represented.

• The size of the significand determines the precision

of the representation.

2.5 Floating-Point Representation

Page 94: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

94

• The IEEE-754 single precision floating point

standard uses an 8-bit exponent and a 23-bit

significand.

• The IEEE-754 double precision standard uses an

11-bit exponent and a 52-bit significand.

2.5 Floating-Point Representation

For illustrative purposes, we will use a 14-bit model

with a 5-bit exponent and an 8-bit significand.

Page 95: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Example IEEE-754 Single-Precision

Floating-Point Numbers

95

Page 96: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Range of IEEE-754

Double-Precision Numbers

96

2127 × 1.1111111111 11111111111112 (let’s call this value MAX)

2–127 × .000000000000000000000012 (let’s call this value MIN).

Single precision floating-point numbers cannot represent:

- negative numbers less than –MAX (negative overflow);

- negative numbers greater than –MIN (negative underflow);

- positive numbers less than +MIN (positive underflow); and

- positive numbers greater than +MAX (positive overflow).

Page 97: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

98

• Example:

– Express 3210 in the simplified 14-bit floating-point

model.

• We know that 32 is 25. So in (binary) scientific

notation 32 = 1.0 x 25 = 0.1 x 26.

• Using this information, we put 110 (= 610) in the

exponent field and 1 in the significand as shown.

2.5 Floating-Point Representation

Page 98: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

99

• Another problem with our system is that we have

made no allowances for negative exponents. We

have no way to express 0.5 (=2 -1)! (Notice that

there is no sign in the exponent field!)

2.5 Floating-Point Representation

All of these problems can be fixed with no

changes to our basic model.

Page 99: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

100

• To provide for negative exponents, we will use a

biased exponent.

• 5 bits แทนไดถง 32 เพอเกบคาลบดวย ถงแบงครง 16 เรยกวา excess-

16

– In our case, we have a 5-bit exponent. We will use 16 for

our bias. This is called excess-16 representation.

2.5 Floating-Point Representation

16 31 0 - +

Page 100: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

101

• Example:

– Express 3210 in the revised 14-bit floating-point model.

• We know that 32 = 1.0 x 25 = 0.1 x 26.

• To use our excess 16 biased exponent, we add 16 to

6, giving 2210 (=101102).

• Graphically:

2.5 Floating-Point Representation

Page 101: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

102

• Example:

– Express 0.062510 in the revised 14-bit floating-point

model.

• We know that 0.0625 is 2-4. So in (binary) scientific

notation 0.0625 = 1.0 x 2-4 = 0.1 x 2 -3.

• To use our excess 16 biased exponent, we add 16 to

-3, giving 1310 (=011012).

2.5 Floating-Point Representation

Page 102: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

103

• Example:

– Express -26.62510 in the revised 14-bit floating-point

model.

• We find 26.62510 = 11010.1012. Normalizing, we

have: 26.62510 = 0.11010101 x 2 5.

• To use our excess 16 biased exponent, we add 16 to

5, giving 2110 (=101012). We also need a 1 in the sign

bit.

2.5 Floating-Point Representation

Page 103: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

104

• Calculations aren’t useful until their results can

be displayed in a manner that is meaningful to

people.

• We also need to store the results of calculations,

and provide a means for data input.

• Thus, human-understandable characters must be

converted to computer-understandable bit

patterns using some sort of character encoding

scheme.

2.6 Character Codes

Page 104: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

รหสแทนขอมลภายในเครองคอมพวเตอร

• รหสแทนขอมลทเปนตวเลข – Sign and Magnitude

– 1’s Complement

– 2’s Complement

• รหสแทนขอมลทเปนตวอกษร – BCD : Binary Code Decimal

– EBCDIC : Extended Binary Code Decimal Interchange

Code

– ASCII : American Standard Code Interchange International

Page 105: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

จ านวนแบบตวอกขระในระบบคอมพวเตอร นอยสด

• ตวเลข 10 ตว ( 0, 1 ,2 … 9)

• ตวอกษรภาษาองกฤษ 26 ตว ( A,B,…, Z)

• เครองหมายพเศษ 16 ตว ( + - * / > < ; ,!@#$%&() )

รวมทงหมด 52 ตว สามารถใชกลมเลขฐานสองเพยง 6 bits

ในระบบ BCD เพม Parity bit อก 1 bit เพอการตรวจสอบความถกตอง

Page 106: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

รหสแทนขอมลทเปนตวอกษร แบบ BCD

• ใน 1 Byte ม 7 Bits

BA Numeric แทน 00 0 - 9 0 - 9 11 1 - 9 A - I 10 1 - 9 J - R 01 2 - 9 S - Z

• อกขระพเศษแตละบรษทก าหนดเอง

C B A 8 4 2 1

Numeric Zone

Parity

Page 107: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Binary-Coded Decimal

108

Digit BCD

0

1

2

3

4

5

6

7

8

9

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

Zones

1111

1100

1101

Unsigned

Positive

Negative

Page 108: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

BCD

• For example, to encode 146, the decimal

digits are replaced by 0001, 0100, and 0110,

respectively.

• Computers use bytes as the smallest unit of

access, most values are stored in 8 bits, not

4. Using this approach, padding with zeros,

146 would be stored as 00000001 00000100

00000110.

109

Page 109: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

packed BCD

110

Page 110: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

รหสแทนขอมลทเปนตวอกษร แบบ EBCDIC

• 1 Byte ใช 8 bits

• แทนอกขระได 256 ตว 8 4 2 1 8 4 2 1

Zone Numeric

Page 111: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

EBCDIC Code

112

Page 112: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

American Standard Code

for Information Interchange (ASCII)

• ออกโดย สมาคมรกษามาตรฐานของ สหรฐอเมรกา • เพอใหการตดตอสอสารระหวางเครองคอมพวเตอร มความสะดวก

และเปนมาตรฐานเดยวกน

• 1 Byte ใช 8 bit

• มใชในเครองไมโครคอมพวเตอร ทดสอบการแทนรหสไดโดยการกด Alt + Numeric Key Pad แทนรหส ASCII

Page 113: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

114

รหสแทนขอมลทเปนตวอกษร แบบ ASCII

Page 114: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Parity bit

ใชในการตรวจสอบความผดพลาด โดยกระท าภายใตขอสมมตฐาน 2 ขอ • คาของความผดพลาดทจะเกดม Percent นอยมาก • ความผดพลาดทเกดขน เกดไดเพยง bit เดยวเทานนใน 1 byte

Check Parity Bit ม 2 แบบ

• Odd Parity จ านวน bit ค • Even Parity จ านวน bit ค

Page 115: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

116

Parity bit

Page 116: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

รหสแทนขอมลทเปนตวอกษร แบบ ASCII

117

Page 117: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Unicode

• In 1991, to establish a new international

information exchange code called Unicode.

• Unicode is a 16-bit alphabet that is downward

compatible with ASCII and the Latin-1

character set.

• Conformant with the ISO/IEC 10646-1

international alphabet.

• Capacity to encode the majority of characters

used in every language of the world. 118

Page 118: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

Unicode Codespace

119

Page 119: Chapter 2staff.cs.psu.ac.th/sathit/ComOr/Ch02-s.pdf · 10 = (10001.11001) 2 23 2.3 Decimal to Binary Conversions.8 x 2 = 1.6 ... Binary Decimal 0 0001001 1 1110110 1 1011000 0 1111111

120

End of Chapter 2