lec2 intro to computer engineering by hsien-hsin sean lee georgia tech -- number system
TRANSCRIPT
ECE2030 Introduction to Computer Engineering
Lecture 2: Number System
Prof. Hsien-Hsin Sean LeeProf. Hsien-Hsin Sean LeeSchool of Electrical and Computer EngineeringSchool of Electrical and Computer EngineeringGeorgia TechGeorgia Tech
22
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?
9013490134109013901310 44 90190110 33 909010 11 99 00
33
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 N w/
base-bbNN = An-1 An-2 … A1 A0
NN = An-1*bn-1 + An-2*bn-2 + … + A2*b2 + A0*b0
**Note that A < b**Note that A < b
44
Counting numbers with base-bb
00112233445566778899
1010111112121313141415151616171718181919
Base-10
9090919192929393949495959696979798989999
…..
100100101101102102103103104104105105106106107107108108109109
How about Base-8
0011223344556677
10101111121213131414151516161717
20202121222223232424252526262727
70707171727273737474757576767777
…..
100100101101102102103103104104105105106106107107
2020212122222323242425252626272728282929
55
How about base-22
0011
10101111
100100101101110110111111
1000100010011001101010101011101111001100110111011110111011111111
66
How about base-22
0 0 1 1
10101111
100100101101110110111111
1000100010011001101010101011101111001100110111011110111011111111
77
How about base-22
0 0 = 0= 01 1 = 1= 1
10 10 = 2= 211 11 = 3= 3
100 100 = 4= 4101 101 = 5= 5110 110 = 6= 6111 111 = 7= 7
1000 1000 = 8= 81001 1001 = 9= 91010 1010 = 10= 101011 1011 = 11= 111100 1100 = 12= 121101 1101 = 13= 131110 1110 = 14= 141111 1111 = 15= 15
Binary Binary == DecimalDecimal
88
Derive Numbers in Base-2 • Decimal (base-10)
– (25)10
• Binary (base-2)– (11001)2
• Exercise
252521212 2 11 66 2 00 33 2 00 11 11
1111
Base 16• 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
00112233445566778899aabbccddeeff
1212
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 0xfe8a7d20 ((0x 0x is an abbreviation of Hex))
– Or in binary form Or in binary form 1111_1110_1000_1010_0111_1101_0010_0000 1111_1110_1000_1010_0111_1101_0010_0000
1313
Number Examples with Different Bases• 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
1414
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
3939666 6 33 11 00
(0100111)2 = (103)6
1616
Negative Number Representation• Options
– Sign-magnitude– One’s Complement– Two’s Complement (we use this in this
course)
1717
Sign-magnitude• Use the most significant bit
(MSB) to indicate the sign– 00: positive, 11: negative
• Problem– Representing zeros?– Do not work in computation
• We will NOT use it in this course !
+0 000+1 001+2 010+3 011-3 111-2 110-1 1010 100
1818
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 !
+0 000+1 001+2 010+3 011-3 100-2 101-1 1100 111
1919
Two’s Complement• ComplementComplement (flip) each bit in a
binary number and adding 1adding 1, with overflow ignored
• Work in computation perfectly• We will use it in this course !
011
100One’s complement
3
101Add 1
-3
010One’s complement
101-3
011Add 1
3
2020
Two’s Complement• ComplementComplement (flip) each bit in a
binary number and adding 1adding 1, with overflow ignored
• Work in computation perfectly• We will use it in this course !
0 000+1 001-1 111+2 010-2 110+3 011-3 101?? 100
100
011One’s complement
100Add 1
The same 100 representsboth 4 and -4 which is no good
2121
Two’s Complement• ComplementComplement (flip) each bit in a
binary number and adding 1adding 1, with overflow ignored
• Work in computation perfectly• We will use it in this course !
0 000+1 001-1 111
1+2 010-2 111
0+3 011-3 110
1--4 110
0
100
011One’s complement
100Add 1
MSB = 1 for negative Number, thus 100 represents -4
2222
Range of Numbers • An N-bit number
– Unsigned: 0 .. (2N -1)– Signed: -2N-1.. (2N-1 -1)
• Example: 4-bit
1110 (-8) 0111 (7)Signed numbers
0000 (0) 1111 (15)Unsigned numbers
2323
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)
2424
Binary ComputationUnsigned arithmetic 101111 (47)011111 (31)---------------001110 (78?? Due to overflow, note that 62 cannot be represented by a 6-bit unsigned number)
The carry isdiscarded
Signed arithmetic (w/ 2’s complement) 101111 (-17 since 2’s complement=010001)011111 (31)---------------001110 (14)
The carry isdiscarded
2626
Application of Two’s Complement• The first Pocket CalculatorPocket Calculator
“Curta” used Two’s complement method for subtractionsubtraction
• 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
2727
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)