Top Banner
ALGORITHMS AND PROGRAMMING CSC 2201 Dokuz Eylul University, Faculty of Science, Department of Mathematics -4-
40

ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

May 17, 2018

Download

Documents

vucong
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: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

ALGORITHMS AND PROGRAMMING

CSC 2201 Dokuz Eylul University, Faculty of Science,

Department of Mathematics

-4-

Page 2: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Number Systems

System Base Symbols

Used by

humans?

Used in

computers?

Decimal 10 0, 1, … 9 Yes No

Binary 2 0, 1 No Yes

Octal 8 0, 1, … 7 No No

Hexa-

decimal

16 0, 1, … 9,

A, B, … F

No No

2

Page 3: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Quantities/Counting

Decimal Binary Octal

Hexa-

decimal

0 0 0 0

1 1 1 1

2 10 2 2

3 11 3 3

4 100 4 4

5 101 5 5

6 110 6 6

7 111 7 7

3

Page 4: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Quantities/Counting

Decimal Binary Octal

Hexa-

decimal

8 1000 10 8

9 1001 11 9

10 1010 12 A

11 1011 13 B

12 1100 14 C

13 1101 15 D

14 1110 16 E

15 1111 17 F

4

Page 5: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Quantities/Counting

Decimal Binary Octal

Hexa-

decimal

16 10000 20 10

17 10001 21 11

18 10010 22 12

19 10011 23 13

20 10100 24 14

21 10101 25 15

22 10110 26 16

23 10111 27 17

Etc.

5

Page 6: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Decimal to Octal

Hexadecimal

Decimal Octal

Binary

6

Page 7: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Decimal to Octal

Technique

Divide by 8

Keep track of the remainder

7

Page 8: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

123410 = ?8

123410 = 23228

8

Page 9: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:9

• 89110 = (?)8

Answer: 1573

• 179210 = (?)8

Answer: 3400

Page 10: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Decimal to Hexadecimal

Hexadecimal

Decimal Octal

Binary

10

Page 11: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Decimal to Hexadecimal

Technique

Divide by 16

Keep track of the remainder

11

Page 12: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

123410 = ?16

123410 = 4D216

12

Page 13: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:13

• 112810 = (?)16

Answer: 468

• 31754710 = (?)16

Answer: 4D86B

Page 14: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Binary to Octal

Hexadecimal

Decimal Octal

Binary

14

Page 15: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Binary to Octal

Technique

Group bits in threes, starting on right

Convert to octal digits

15

Page 16: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

10110101112 = ?8

10110101112 = 13278

16

Page 17: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:17

• 1100011002 = (?)8

Answer: 614

• 10101012 = (?)8

Answer: 125

Page 18: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Binary to Hexadecimal

Hexadecimal

Decimal Octal

Binary

18

Page 19: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Binary to Hexadecimal

Technique

Group bits in fours, starting on right

Convert to hexadecimal digits

19

Page 20: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

10101110112 = ?16

10101110112 = 2BB16

20

Page 21: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:21

• 010011102 = (?)16

Answer: 4E

• 01001010000000012 = (?)16

Answer: 4A01

Page 22: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Octal to Hexadecimal

Hexadecimal

Decimal Octal

Binary

22

Page 23: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Octal to Hexadecimal

Technique

Use binary as an intermediary

23

Page 24: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

10768 = ?16

10768 = 23E16

24

Page 25: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:25

• 10028 = (?)16

Answer: 202

• 25248 = (?)16

Answer:554

Page 26: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Hexadecimal to Octal

Hexadecimal

Decimal Octal

Binary

26

Page 27: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Hexadecimal to Octal

Technique

Use binary as an intermediary

27

Page 28: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Example:

1F0C16 = ?8

1F0C16 = 174148

28

Page 29: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise:29

• B7816 = (?)8

Answer: 5570

• BAC916 = (?)8

Answer: 135311

Page 30: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise – Convert ...

Decimal Binary Octal

Hexa-

decimal

33

1110101

703

1AF

30

Page 31: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Exercise – Convert …

Decimal Binary Octal

Hexa-

decimal

33 100001 41 21

117 1110101 165 75

451 111000011 703 1C3

431 110101111 657 1AF

31

Page 32: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Data Representation

How are data (integers in particular) actually

represented in computers?

Straight-forward binary representation is convenient

for non-negative integers.

How about negative integers?

(See IEEE-754 Floating Point Standard to find out how real numbers are

represented in computers)

32

Page 33: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Data Representation

Signed Magnitude Representation

Preserve a bit to indicate the sign, i.e. use a sign-bit.

110 = 12 510 = 1012

1 = [ 0001 ] 5 = [ 0101 ]

-1 = [ 1001 ] -5 = [ 1101 ]

Do not mistake the number 11012 for 1310!

Easy to represent, hard to perform arithmetic (requires

subtraction circuitry).

33

Page 34: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Data Representation

Can we convert our subtraction problems to addition? Yes.

1's complement

1. First, we write the positive value of the number in binary: 0101 (+5)

2. Next, we reverse each bit of the number so 1's become 0's and 0's

become 1's: 1010 (-5)

2's complement

1. First, we write the positive value of the number in binary: 0101 (+5)

2. Next, we reverse each bit to get the 1's complement: 1010

3. Last, we add 1 to the number: 1011 (-5)

Which one is preferable? You choose!

34

Page 35: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Data Representation

1's complement 2's complement

35

Page 36: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Data Representation

Subtracting y from x

36

1’s complement

1. Convert y to -y

2. Add -y to x

3. Add the overflowing

bit to the result

2’s complement

1. Convert y to -y

2. Add -y to x

3. Ignore the

overflowing bit

Page 37: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

2 – 5 = ? (4-bit signed representation)

Example:37

1’s complement

1. 00102 = 1

10102 = -5

2. 00102 + 10102

= 11002

3. No overflow.

11002 = -3

2’s complement

1. 00102 = 1

10112 = -5

2. 00102 + 10112

= 11012

3. No overflow.

11012 = -3

Page 38: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

7 – 3 = ? (4-bit signed representation)

Example:38

1’s complement

1. 01112 = 7

11002 = -3

2. 01112 + 11002

= 100112

3. 00112 + 12

= 01002 = 4

2’s complement

1. 01112 = 7

11012 = -3

2. 01112 + 11012

= 101002

3. Ignore overflow

01002 = 4

Page 39: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement

Perform the operations with 5-bit signed integers

using both 1’s and 2’s complement.

1. 10 + 2 = ?

2. 3 – 7 = ?

3. 15 – 1 = ?

Exercise:39

1’s complement

011002

110112

011102

2’s complement

011002

111002

011102

Page 40: ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData Representation Can we convert our subtraction problems to addition? Yes. 1's complement