ALGORITHMS AND PROGRAMMING - Ki??isel Sayfalarkisi.deu.edu.tr/fidan.nuriyeva/CSC2201_04(3).pdfData...
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
Binary to Hexadecimal
Technique
Group bits in fours, starting on right
Convert to hexadecimal digits
19
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
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