ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData...

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

Transcript of ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData...

ALGORITHMS AND PROGRAMMING

CSC 2201 Dokuz Eylul University, Faculty of Science,

Department of Mathematics

-4-

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

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

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

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

Decimal to Octal

Hexadecimal

Decimal Octal

Binary

6

Decimal to Octal

Technique

Divide by 8

Keep track of the remainder

7

Example:

123410 = ?8

123410 = 23228

8

Exercise:9

• 89110 = (?)8

Answer: 1573

• 179210 = (?)8

Answer: 3400

Decimal to Hexadecimal

Hexadecimal

Decimal Octal

Binary

10

Decimal to Hexadecimal

Technique

Divide by 16

Keep track of the remainder

11

Example:

123410 = ?16

123410 = 4D216

12

Exercise:13

• 112810 = (?)16

Answer: 468

• 31754710 = (?)16

Answer: 4D86B

Binary to Octal

Hexadecimal

Decimal Octal

Binary

14

Binary to Octal

Technique

Group bits in threes, starting on right

Convert to octal digits

15

Example:

10110101112 = ?8

10110101112 = 13278

16

Exercise:17

• 1100011002 = (?)8

Answer: 614

• 10101012 = (?)8

Answer: 125

Binary to Hexadecimal

Hexadecimal

Decimal Octal

Binary

18

Binary to Hexadecimal

Technique

Group bits in fours, starting on right

Convert to hexadecimal digits

19

Example:

10101110112 = ?16

10101110112 = 2BB16

20

Exercise:21

• 010011102 = (?)16

Answer: 4E

• 01001010000000012 = (?)16

Answer: 4A01

Octal to Hexadecimal

Hexadecimal

Decimal Octal

Binary

22

Octal to Hexadecimal

Technique

Use binary as an intermediary

23

Example:

10768 = ?16

10768 = 23E16

24

Exercise:25

• 10028 = (?)16

Answer: 202

• 25248 = (?)16

Answer:554

Hexadecimal to Octal

Hexadecimal

Decimal Octal

Binary

26

Hexadecimal to Octal

Technique

Use binary as an intermediary

27

Example:

1F0C16 = ?8

1F0C16 = 174148

28

Exercise:29

• B7816 = (?)8

Answer: 5570

• BAC916 = (?)8

Answer: 135311

Exercise – Convert ...

Decimal Binary Octal

Hexa-

decimal

33

1110101

703

1AF

30

Exercise – Convert …

Decimal Binary Octal

Hexa-

decimal

33 100001 41 21

117 1110101 165 75

451 111000011 703 1C3

431 110101111 657 1AF

31

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

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

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

Data Representation

1's complement 2's complement

35

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

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

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

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