Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1301 Computer Programming Lecture 32: Number Representation

Similar presentations


Presentation on theme: "CSE1301 Computer Programming Lecture 32: Number Representation"— Presentation transcript:

1 CSE1301 Computer Programming Lecture 32: Number Representation
CSE1301 S1-2001 4/26/2017 CSE1301 Computer Programming Lecture 32: Number Representation Lecture 33: Numerical 1

2 Topics Representing characters and integers
Converting binary to decimal Converting decimal to binary Adding binary numbers Limitations

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)

4 Representing Integers
We represent integers using a base-10 positional notation. So the number means: 8    100

5 Representing Integers
Computers represent integers using a base-2 positional notation. So the number means: 125 + 024 + 123 + 022 + 121 + 120 132 + 016 + 18 + 04 + 1 1 = 43 in decimal

6 Representing Integers
Curiously, ye Olde Englishe pub-keepers used the same system: So the number means: 1gallon + 0pottle + 1quart + 0pint chopin + 1gill 14.6l + 02.3l + 11.1l + 00.6l 0.3l + 10.1l = 6.1 litres

7 Representing Integers
The first few binary numbers are:

8 Converting binary to decimal
Example: Convert the unsigned binary number to decimal. 1

9 Converting binary to decimal
1 2 3 4 5 6 7 1 1 1 1 2 1 3 4 5 6 7

10 Converting binary to decimal
1 2 3 4 5 6 7 1 1 1 1 64 1 2 4 8 16 32 128

11 Converting binary to decimal
7 6 5 4 3 2 1 1 1 1 1 128 64 32 16 8 4 2 1 = 154 (or ) So, in unsigned binary is 154 in decimal.

12 Converting Decimal to/from Binary
Analogy: Giving a $69.10 change from largest to smallest denomination: 69.10 = 1  $50 bill 19.10 = 1  $10 bill 9.10 = 1  $5 bill 4.10 = 2  $2 coin 0.10 = 1  10c coin

13 Converting Decimal to/from Binary
Bit position. 7 6 5 4 3 2 1 2 1 3 4 6 7 2 5 Decimal value.

14 Converting Decimal to/from Binary
Bit position. 7 6 5 4 3 2 1 128 64 32 16 8 4 2 1 Decimal value.

15 Decimal to Binary Example: Convert the decimal number 105 to unsigned binary.

16 1 Q. What is the highest power of 2 that is less than or equal to 105?
1 2 3 4 5 6 1 128 64 32 16 8 4 2 1 Next, consider the difference: = 41

17 Q. What is the highest power of 2 that is less than or equal to 41?
6 5 4 3 2 1 1 1 64 32 16 8 4 2 1 Next, consider the difference: = 9

18 Q. What is the highest power of 2 that is less than or equal to 9?
6 5 4 3 2 1 1 1 1 64 32 16 8 4 2 1 Next, consider the difference: = 1

19 Q. What is the highest power of 2 that is less than or equal to 1?
6 5 4 3 2 1 1 1 1 1 64 32 16 8 4 2 1 Next, consider the difference: = 0 Done!

20 1 1 1 1 Finally, fill the empty boxes with zeros.
CSE1301 S1-2001 4/26/2017 Finally, fill the empty boxes with zeros. 6 5 4 3 2 1 1 1 1 1 64 32 16 8 4 2 1 So, 105 decimal is in unsigned binary. Lecture 33: Numerical 1

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.

22 Converting decimal to binary
123 ÷2 61  remainder 1 30  remainder 1 15  remainder 0 7  remainder 1 3  remainder 1 1  remainder 1 0  remainder 1

23 Converting decimal to binary
123 ÷2 61  remainder 1 30  remainder 1 15  remainder 0 7  remainder 1 3  remainder 1 1  remainder 1 0  remainder 1

24 Converting decimal to binary
÷2 61  remainder 1 30  remainder 1 15  remainder 0 7  remainder 1 3  remainder 1 1  remainder 1 0  remainder 1

25 Converting decimal to binary
102 ÷2 51  remainder 0 25  remainder 1 12  remainder 1 6  remainder 0 3  remainder 0 1  remainder 1 0  remainder 1

26 Converting decimal to binary
102 ÷2 51  remainder 0 25  remainder 1 12  remainder 1 6  remainder 0 3  remainder 0 1  remainder 1 0  remainder 1

27 Converting decimal to binary
÷2 51  remainder 0 25  remainder 1 12  remainder 1 6  remainder 0 3  remainder 0 1  remainder 1 0  remainder 1

28 Converting decimal to binary
÷2 51  remainder 0 25  remainder 1 12  remainder 1 6  remainder 0 3  remainder 0 1  remainder 1 0  remainder 1 Most significant bit

29 Converting decimal to binary
÷2 51  remainder 0 25  remainder 1 12  remainder 1 6  remainder 0 3  remainder 0 1  remainder 1 0  remainder 1 Least significant bit

30 Adding binary numbers For individual bits there are only four possibilities:

31 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

32 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos.

33 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

34 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

35 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

36 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

37 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

38 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

39 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:

40 Adding binary numbers For multiple digits we do the same as in base-10: we add corresponding bits and carry the twos:    322

41 Adding binary numbers But that only works for unsigned numbers. (positive numbers) For signed numbers it's much more complicated.

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!)

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 Advanced Exercise for examples.)

44 Reading Brookshear: Sections 1.4 to 1.7 D&D: Appendix E


Download ppt "CSE1301 Computer Programming Lecture 32: Number Representation"

Similar presentations


Ads by Google