Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Mathematics of radio engineering wikipedia , lookup
Approximations of π wikipedia , lookup
Large numbers wikipedia , lookup
Real number wikipedia , lookup
Elementary arithmetic wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Elementary mathematics wikipedia , lookup
Location arithmetic wikipedia , lookup
Computer Organization Integers and Arithmetic Operations Terrence Mak Counting to 9, oh no, 1 please • Binary numbers (0, 1) are commonly used in computers as they are easily represented as on/off electrical signals – Other related radix systems are Octal & Hexadecimal • Different number (representation) systems are used in computers • Let's assume n bits (binary digits) are used in the following discussions Only 0 and 1 in a Sequence • Numbers are represented as binary vectors B = bn-1 bn-2 … b1 b0 • MSB = Most Significant Bit = bn-1 i.e. leftmost digit in a binary vector • LSB = Least Significant Bit = b0 i.e. rightmost digit in a binary vector Representing Non-negative Numbers • Unsigned (non-negative) numbers are in range 0 to 2n – 1 • Represented by Value(B) = bn-12n-1 + … + b121 + b020 if B is a binary vector representing an unsigned integer Representing Signed Numbers • In written decimal system, a signed number is "usually" represented by a "dash" or "plus" sign and followed by the magnitude e.g. –73, –215, +349 • In binary system, we have several choices: – Sign-and-magnitude – 1’s complement – 2’s complement Representing Signed Numbers • Sign-and-magnitude – MSB determines sign, remaining unsigned bits represent magnitude – MSB: 0 means "+", 1 means "–" • 1’s complement – MSB determines sign – To change sign from unsigned to negative, invert all the bits • 2’s complement (commonly used) – MSB determines sign – To change sign from unsigned to negative, invert all the bits and then add 1 – This is equivalent to subtracting the positive number from 2n Signed Number Systems B (n = 4) b 3 b 2 b1 b 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 Values represented Sign and magnitude 1's complement +7 +6 +5 +4 +3 +2 +1 +0 - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 +7 +6 +5 +4 +3 +2 +1 +0 -7 -6 -5 -4 -3 -2 - 1 -0 2's complement + + + + + + + + - 7 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 2’s Complement • 2's complement numbers actually make sense since they follow normal modulo arithmetic except when they overflow • Range is –2(n–1) to +2(n–1) – 1 e.g. –8 to +7 for n = 4 N- 1 N-2 0 0000 1111 1 1110 2 1101 1100 -1 0010 +1 - 2 - 3 -4 -5 1011 +2 +3 0011 Overflow + 4 cut-off + 5 - 6 +6 - 7 - 8 +7 1010 1001 (a) Circle representation of integers mod N 0 0001 1000 0100 0101 0110 0111 (b) Mod 16 system for 2's-complement numbers for n = 4, N = 2n = 24 = 16 1-bit Addition 0 + 0 0 1 + 0 1 0 + 1 1 1 + 1 10 Carry-out n-bit Addition/ Subtraction • X+Y – Use 1-bit addition propagating carry to the most significant bit • X–Y X + (– Y) – Add X to the 2’s complement of Y 4-bit Addition/ Subtraction of numbers represented in 2’s Complement (a) (c) (e) 0100 +1010 (+ 4) ( - 6) 1110 ( - 2) 0111 +1101 (+7) (- 3) (- 7) 0100 (+4) (- 3) (- 7 ) 1101 +0111 0010 +0011 ( + 2) ( + 3) 0101 ( + 5) 1011 +1110 (- 5) (- 2) 1001 1101 - 1001 (b) (d) 0100 (f) 0010 - 0100 (+ 2) (+4) (+4) 0010 +1100 1110 (- 2) 4-bit Addition/ Subtraction of numbers represented in 2’s Complement (g) 0110 - 0011 (+ 6 ) (+ 3 ) 0110 +1101 0011 (h) 1001 - 1011 ( - 7) ( - 5) 1001 +0101 1110 (i) 1001 - 0001 ( -7) ( +1 ) 0010 - 1101 ( +2) ( - 3) ( - 2) 1001 +1111 1000 (j) (+3) ( - 8) 0010 +0011 0101 (+ 5) Sign Extension • Given a 4-bit 2’s complement number • Make it into an 8-bit 2's complement number – Positive number • add 0’s to LHS • e.g. 0111 00000111 – Negative number • add 1’s to LHS • e.g. 1010 11111010 – c.f. circle representation, i.e. extend the MSB Overflow • In 2’s complement arithmetic – Addition of opposite sign numbers never overflow – If the numbers are the same sign and the result is the opposite sign, overflow has occurred • e.g. 0111 + 0100 = 1011 = –5! • positive + positive = negative?! • In unsigned arithmetic – Carry out signals an overflow Exercise • Using 4-bit 2’s complement number system, – – – – What is the binary for -2? Calculate 2+3 Calculate -2-3 Calculate 5+5 • Using 5-bit number system – What is the largest 2’s complement number? – What is the smallest 2’s complement number? – What is the largest unsigned number? • Convert 56 to unsigned binary • What is the decimal value of 10110101 in 2’s complement? • What is the decimal value of unsigned number 10110101? Characters - 8-bit ASCII representation