Top Banner
1 CSE1301 Computer Programming Lecture 32: Number Representation
44

CSE1301 Computer Programming Lecture 32: Number Representation

Jan 17, 2018

Download

Documents

Stephen Pearson

Topics Representing characters and integers Converting binary to decimal Converting decimal to binary Adding binary numbers Limitations
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE1301 Computer Programming Lecture 32: Number Representation

1

CSE1301Computer Programming

Lecture 32:Number Representation

Page 2: CSE1301 Computer Programming Lecture 32: Number Representation

2

Topics

• Representing characters and integers• Converting binary to decimal• Converting decimal to binary• Adding binary numbers• Limitations

Page 3: CSE1301 Computer Programming Lecture 32: Number Representation

3

Representing Characters

• ASCII encoding (American Standard Code for Information Interchange)

• Each character represented by a one-byte (8-bit) number

• Other representations possible too:– EBCDIC (used by older IBM machines)– Unicode (16-bit character codes, provides

enough codes for characters from all modern languages)

Page 4: CSE1301 Computer Programming Lecture 32: Number Representation

4

Representing Integers

• We represent integers using a base-10 positional notation.

• So the number 80710 means:8104 + 0103 + 7102 + 1101 + 0100

Page 5: CSE1301 Computer Programming Lecture 32: Number Representation

5

Representing Integers

• Computers represent integers using a base-2 positional notation.

• So the number 101011 means:

125 + 024 + 123 + 022 + 121 + 120

132 + 016 + 18 + 04 + 12 + 11

= 43 in decimal

Page 6: CSE1301 Computer Programming Lecture 32: Number Representation

6

Representing Integers

• Curiously, ye Olde Englishe pub-keepers used the same system:

• So the number 101011 means:

1gallon + 0pottle + 1quart + 0pint + 1chopin + 1gill

14.6l + 02.3l + 11.1l + 00.6l + 10.3l + 10.1l

= 6.1 litres

Page 7: CSE1301 Computer Programming Lecture 32: Number Representation

7

Representing Integers• The first few binary numbers are:

0000....................00001....................10010....................20011....................30100....................40101....................50110....................60111....................71000....................81001....................91010....................101011....................111100....................121101....................131110....................141111....................15

Page 8: CSE1301 Computer Programming Lecture 32: Number Representation

8

Converting binary to decimal

Example: Convert the unsigned binary number 10011010 to decimal.

01011001

Page 9: CSE1301 Computer Programming Lecture 32: Number Representation

9

Converting binary to decimal

01011001

2021222324252627

01234567

Page 10: CSE1301 Computer Programming Lecture 32: Number Representation

10

Converting binary to decimal

0101100101234567

64 12481632128

Page 11: CSE1301 Computer Programming Lecture 32: Number Representation

11

Converting binary to decimal

0101100101234567

64 12481632128

128 + 16 + 8 + 2 = 154 (or 27 + 24 + 23 + 21)

So, 10011010 in unsigned binary is 154 in decimal.

Page 12: CSE1301 Computer Programming Lecture 32: Number Representation

12

Converting Decimal to/from Binary

Analogy: Giving a $69.10 change from largest to smallest denomination:

69.10 = 1 $50 bill + 19.10

19.10 = 1 $10 bill + 9.10

9.10 = 1 $5 bill + 4.10

4.10 = 2 $2 coin + 0.10

0.10 = 1 10c coin

Page 13: CSE1301 Computer Programming Lecture 32: Number Representation

13

25 20212223242627

01234567

Converting Decimal to/from Binary

Bit position.

Decimal value.

Page 14: CSE1301 Computer Programming Lecture 32: Number Representation

14

1248163264128

01234567

Converting Decimal to/from Binary

Bit position.

Decimal value.

Page 15: CSE1301 Computer Programming Lecture 32: Number Representation

15

Decimal to Binary

Example: Convert the decimal number 105 to unsigned binary.

Page 16: CSE1301 Computer Programming Lecture 32: Number Representation

16

Q. What is the highest power of 2 that is less than or equal to 105?

A. 64

164 12481632

0123456

Next, consider the difference: 105 - 64 = 41

128

Page 17: CSE1301 Computer Programming Lecture 32: Number Representation

17

1

Q. What is the highest power of 2 that is less than or equal to 41?

A. 32

11248163264

0123456

Next, consider the difference: 41 - 32 = 9

Page 18: CSE1301 Computer Programming Lecture 32: Number Representation

18

1

Q. What is the highest power of 2 that is less than or equal to 9?

A. 8

1 11248163264

0123456

Next, consider the difference: 9 - 8 = 1

Page 19: CSE1301 Computer Programming Lecture 32: Number Representation

19

1

Q. What is the highest power of 2 that is less than or equal to 1?

A. 1

11 11248163264

0123456

Next, consider the difference: 1 - 1 = 0 Done!

Page 20: CSE1301 Computer Programming Lecture 32: Number Representation

20

1

Finally, fill the empty boxes with zeros.

1001011248163264

0123456

So, 105 decimal is 1101001 in unsigned binary.

Page 21: CSE1301 Computer Programming Lecture 32: Number Representation

21

Converting decimal to binary

• We've seen that you convert binary to decimal by multiplying and adding.

• So it's no surprise that you convert decimal to binary by dividing and subtracting.

• You can also convert decimal to binary by dividing and noting the remainder.

Page 22: CSE1301 Computer Programming Lecture 32: Number Representation

22

Converting decimal to binary123 ÷2

61 remainder 1 ÷2

30 remainder 1 ÷2

15 remainder 0 ÷2

7 remainder 1 ÷2

3 remainder 1 ÷2

1 remainder 1 ÷2

0 remainder 1

Page 23: CSE1301 Computer Programming Lecture 32: Number Representation

23

Converting decimal to binary123 ÷2

61 remainder 1 ÷2

30 remainder 1 ÷2

15 remainder 0 ÷2

7 remainder 1 ÷2

3 remainder 1 ÷2

1 remainder 1 ÷2

0 remainder 1

Page 24: CSE1301 Computer Programming Lecture 32: Number Representation

24

Converting decimal to binary123 1111011 ÷261 remainder 1 ÷230 remainder 1 ÷215 remainder 0 ÷27 remainder 1 ÷23 remainder 1 ÷21 remainder 1 ÷20 remainder 1

Page 25: CSE1301 Computer Programming Lecture 32: Number Representation

25

Converting decimal to binary102 ÷2

51 remainder 0 ÷2

25 remainder 1 ÷2

12 remainder 1 ÷2

6 remainder 0 ÷2

3 remainder 0 ÷2

1 remainder 1 ÷2

0 remainder 1

Page 26: CSE1301 Computer Programming Lecture 32: Number Representation

26

Converting decimal to binary102 ÷2

51 remainder 0 ÷2

25 remainder 1 ÷2

12 remainder 1 ÷2

6 remainder 0 ÷2

3 remainder 0 ÷2

1 remainder 1 ÷2

0 remainder 1

Page 27: CSE1301 Computer Programming Lecture 32: Number Representation

27

102 1100110 ÷2

51 remainder 0 ÷2

25 remainder 1 ÷2

12 remainder 1 ÷2

6 remainder 0 ÷2

3 remainder 0 ÷2

1 remainder 1 ÷2

0 remainder 1

Converting decimal to binary

Page 28: CSE1301 Computer Programming Lecture 32: Number Representation

28

102 1100110 ÷251 remainder 0 ÷225 remainder 1 ÷212 remainder 1 ÷26 remainder 0 ÷23 remainder 0 ÷21 remainder 1 ÷20 remainder 1

Converting decimal to binary

Most significant

bit

Page 29: CSE1301 Computer Programming Lecture 32: Number Representation

29

102 1100110 ÷251 remainder 0 ÷225 remainder 1 ÷212 remainder 1 ÷26 remainder 0 ÷23 remainder 0 ÷21 remainder 1 ÷20 remainder 1

Converting decimal to binary

Least significant

bit

Page 30: CSE1301 Computer Programming Lecture 32: Number Representation

30

Adding binary numbers• For individual bits there are only four

possibilities:

0 0 1 1 + 0 + 1 + 0 + 1

0 1 1 10

Page 31: CSE1301 Computer Programming Lecture 32: Number Representation

31

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos:

11100101+ 1011101

Page 32: CSE1301 Computer Programming Lecture 32: Number Representation

32

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos.

1 11100101

+ 10111010

Page 33: CSE1301 Computer Programming Lecture 32: Number Representation

33

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos: 1

11100101+ 1011101

10

Page 34: CSE1301 Computer Programming Lecture 32: Number Representation

34

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos:

1 1 11100101

+ 1011101010

Page 35: CSE1301 Computer Programming Lecture 32: Number Representation

35

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos: 1 1 1

11100101+ 1011101

0010

Page 36: CSE1301 Computer Programming Lecture 32: Number Representation

36

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos: 1 1 1 1

11100101+ 1011101

00010

Page 37: CSE1301 Computer Programming Lecture 32: Number Representation

37

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos:

1 1 1 1 1 11100101

+ 1011101000010

Page 38: CSE1301 Computer Programming Lecture 32: Number Representation

38

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos: 1 1 1 1 1 1

11100101+ 1011101

1000010

Page 39: CSE1301 Computer Programming Lecture 32: Number Representation

39

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos: 1 1 1 1 1 1 1

11100101+ 1011101101000010

Page 40: CSE1301 Computer Programming Lecture 32: Number Representation

40

Adding binary numbers• For multiple digits we do the same as in

base-10: we add corresponding bits and carry the twos:

11100101 229

+ 1011101 + 93101000010 322

Page 41: CSE1301 Computer Programming Lecture 32: Number Representation

41

Adding binary numbers• But that only works for unsigned numbers.

(positive numbers)

• For signed numbers it's much more complicated.

Page 42: CSE1301 Computer Programming Lecture 32: Number Representation

42

Limitations of representations• None of these number representations acts

exactly like the integers.

• Infinitely many integers, but only 2N binary numbers for a specific N-bit representation.

• That means some operations will overflow.

• If the program doesn't crash when that happens, it will simply produce wrong answers (which is worse!)

Page 43: CSE1301 Computer Programming Lecture 32: Number Representation

43

Limitations of representations• Computer arithmetic is only an

approximation of integer arithmetic.

• Many of the arithmetic laws you're used to don't always hold. (See Tutorial 11 - Advanced Exercise for examples.)

Page 44: CSE1301 Computer Programming Lecture 32: Number Representation

44

Reading• Brookshear: Sections 1.4 to 1.7

• D&D: Appendix E