310 likes | 459 Views
ECE2030 Introduction to Computer Engineering Lecture 2: Number System. Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech. 90134. 10. 4. 10. 9013. 10. 901. 3. 10. 90. 1. 9. 0. Decimal Number Representation.
E N D
ECE2030 Introduction to Computer EngineeringLecture 2: Number System Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech
90134 10 4 10 9013 10 901 3 10 90 1 9 0 Decimal Number Representation • Example: 90134 (base-10, used by Homo Sapien) = 90000 + 0 + 100 + 30 + 4 = 9*104 + 0*103 + 1*102 + 3*101 + 4*100 • How did we get it?
Generic Number Representation • 90134 = 9*104 + 0*103 + 1*102 + 3*101 + 4*100 • A4 A3 A2 A1 A0 for base-10 (or radix-10) = A4*104 + A3*103 +A2*102 +A1*101 +A0*100 (A is coefficient; b is base) • Generalize for a given number N w/ base-b N = An-1 An-2 …A1 A0 N = An-1*bn-1 + An-2*bn-2 + … +A2*b2 +A0*b0 **Note that A < b
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 10 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27 70 71 72 73 74 75 76 77 100 101 102 103 104 105 106 107 ….. ….. Counting numbers with base-b 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 Base-10 How about Base-8
10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 How about base-2 0 1
How about base-2 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111
How about base-2 0 = 0 1 = 1 10 = 2 11 = 3 100 = 4 101 = 5 110 = 6 111 = 7 1000 = 8 1001 = 9 1010 = 10 1011 = 11 1100 = 12 1101 = 13 1110 = 14 1111 = 15 Binary =Decimal
Decimal (base-10) (25)10 Binary (base-2) (11001)2 Exercise 2 25 1 2 12 2 6 0 2 3 0 1 1 Derive Numbers in Base-2
Decimal (base-10) (982)10 Binary (base-2) (1111010110)2 Exercise Base-2
Decimal (base-10) (982)10 Octal (base-8) (1726)8 Exercise Base 8
Decimal (base-10) (982)10 Hexadecimal (base-16) Hey, what do we do when we count to 10?? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Base 16 0 1 2 3 4 5 6 7 8 9 a b c d e f
Base 16 • (982)10 = (3d6)16 • (3d6)16 can be written as (0011 1101 0110)2 • We use Base-16 (or Hex) a lot in computer world • Ex: A 32-bit address can be written as 0xfe8a7d20 (0x is an abbreviation of Hex) • Or in binary form 1111_1110_1000_1010_0111_1101_0010_0000
Decimal (base-10) (982)10 Binary (base-2) (01111010110)2 Octal (base-8) (1726)8 Hexadecimal (base-16) (3d6)16 Others examples: base-9 = (1321)9 base-11 = (813)11 base-17 = (36d)17 Number Examples with Different Bases
6 39 Convert between different bases • Convert a number base-x to base-y, e.g. (0100111)2 to (?)6 • First, convert from base-x to base-10 if x 10 • Then convert from base-10 to base-y 0100111 = 026 + 125 + 024 + 023 + 122 + 121 + 120 = 39 3 6 6 1 0 (0100111)2 = (103)6
Negative Number Representation • Options • Sign-magnitude • One’s Complement • Two’s Complement (we use this in this course)
Sign-magnitude • Use the most significant bit (MSB) to indicate the sign • 0: positive, 1: negative • Problem • Representing zeros? • Do not work in computation • We will NOT use it in this course !
One’s Complement • Complement (flip) each bit in a binary number • Problem • Representing zeros? • Do not always work in computation • Ex: 111 + 001 = 000 Incorrect ! • We will NOT use it in this course !
-3 101 One’s complement One’s complement 100 010 Add 1 Add 1 -3 3 101 011 Two’s Complement • Complement (flip) each bit in a binary number and adding 1, with overflow ignored • Work in computation perfectly • We will use it in this course ! 3 011
One’s complement 011 The same 100 represents both 4 and -4 which is no good Add 1 100 Two’s Complement • Complement (flip) each bit in a binary number and adding 1, with overflow ignored • Work in computation perfectly • We will use it in this course ! 100
One’s complement 011 MSB = 1 for negative Number, thus 100 represents -4 Add 1 100 Two’s Complement • Complement (flip) each bit in a binary number and adding 1, with overflow ignored • Work in computation perfectly • We will use it in this course ! 100
0000 (0) 1111 (15) Unsigned numbers 1110 (-8) 0111 (7) Signed numbers Range of Numbers • An N-bit number • Unsigned: 0 .. (2N -1) • Signed: -2N-1.. (2N-1 -1) • Example: 4-bit
Binary Computation 010001 (17=16+1) 001011 (11=8+2+1) --------------- 011100 (28=16+8+4) Unsigned arithmetic 010001 (17=16+1) 101011 (43=32+8+2+1) --------------- 111100 (60=32+16+8+4) Signed arithmetic (w/ 2’s complement) 010001 (17=16+1) 101011 (-21: 2’s complement=010101=21) --------------- 111100 (2’s complement=000100=4, i.e. -4)
Unsigned arithmetic 101111 (47) 011111 (31) --------------- 001110 (78?? Due to overflow, note that 62 cannot be represented by a 6-bit unsigned number) The carry is discarded Signed arithmetic (w/ 2’s complement) 101111 (-17 since 2’s complement=010001) 011111 (31) --------------- 001110 (14) The carry is discarded Binary Computation
Application of Two’s Complement • The first Pocket Calculator“Curta” used Two’s complement method for subtraction • First complement the subtrahend • Fill the left digits to be the same length of the minuend • Complemented number = (9 – digit) • 4’s complement = 5 • 7’s complement = 2 • 0’s complement = 9 • Add 1 to the complemented number • Perform an addition with the minuend
Examples • 13 – 7 • Two’s complement of 07 = 92 + 1 = 93 • 13 + 93 = 06 (ignore the leftmost carry digit) • 817 – 123 • Two’s complement of 123 = 876 + 1 = 877 • 817 + 877 = 694 (ignore the leftmost carry digit) • 78291 – 4982 • Two’s complement of 04982 = 95017 + 1 = 95018 • 78291 + 95018 = 73309 (ignore the leftmost carry digit)