Number Systems Binary Arithmetic References Number Systems Binary Arithmetic References Number Systems COMP1021/9021 Lecture 12 We use a base 10 system for representing numbers and arithmetic Number Systems and Binary Arithmetic The Sumerian culture used a base 60 number system The Mayan culture developed a base 20 number system Kai Engelhardt Computers essentially use a base 2 (or binary) number system Base 8 (octal) and base 16 (hexadecimal) number systems are also commonly used in writing programs CSE, UNSW (and NICTA) In this lecture we will look at various number systems, converting between them and also binary arithmetic Revision: 1.2 of Date: 2006/01/13 06:25:53 UTC almost identical to Maurice Pagnucco’s slides 2 1 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Decimal Representation References Binary Representation Can interpret decimal number 4705 as: In a similar way, can interpret binary number 1011 as: 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 4 × 103 + 7 × 102 + 0 × 101 + 5 × 100 The base or radix is 2 Digits 0 and 1 The base or radix is 10 Digits 0 – 9 Place values: ··· ··· Write number as 10112 (= 1110 ) Place values: · · · 1000 ··· 103 Write number as 470510 100 102 10 101 1 100 8 23 4 22 2 21 1 20 Note use of subscript to denote base 3 4 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Binary Representation Binary Representation Addition of 1-bit binary numbers can be achieved by half-adder circuit. Binary representation is convenient because easily represented using two voltage levels: low and high simple arithmetic tables that can be implemented using logic gates + 0 1 0 00 01 × 0 1 0 0 0 References XOR Sum AND Carry 1 01 10 1 0 1 General addition requires combination of half-adder circuits 6 5 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Converting from base x to Decimal Examples Consider n-digit number in arbitrary base b: [d (n−1) . . . d (0) ]x 11012 Rewrite in polynomial form: n−1 X 11012 = 1.23 + 1.22 + 0.21 + 1.20 = 1310 11012 = ((1.2 + 1).2 + 0).2 + 1 = 1310 (d (i) × bi ) 110010112 110010112 = 1.27 + 1.26 + 0.25 + 0.24 + 1.23 + 0.22 + 1.21 + 1.20 = 20310 110010112 = ((((((1.2 + 1).2 + 0).2 + 0).2 + 1).2 + 0).2 + 1).2 + 1 = 20310 i=0 Or nested form: 310425 ((. . . (d (n−1) b + d (n−1) )b + . . . + d (1) )b + d (0) )10 310425 = 3.54 + 1.53 + 0.52 + 4.51 + 2.50 = 202210 310425 = (((3.5 + 1).5 + 0).5 + 4).5 + 2 = 202210 Perform necessary arithmetic to achieve decimal conversion 7 References 8 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Converting to base x Examples Note that a base x number in nested form ((ax + b)x + c)x + d can be written: abcdx C B a = = = = References 2510 ÷ 2 1210 ÷ 2 610 ÷ 2 310 ÷ 2 110 ÷ 2 Cx + d Bx + c ax + b 0x + a = = = = = 12 6 3 1 0 r r r r r ↑ ↑ ↑ ↑ ↑ 1 0 0 1 1 Write remainders from left to right reading from bottom to top. −→ 2510 = 110012 Numbers a, b, c, d are all less than base x Equations above imply that d , c, b, a are remainders when the number abcdx is repeatedly divided by x To convert an integer to base x Check: 110012 = 1.24 + 1.23 + 0.22 + 0.21 + 1.20 = 2510 repeatedly divide quotient by x until quotient is 0 write the remainders in reverse . . . or verify your conversion routines. 9 10 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Examples 4873210 ÷ 5 9746 ÷ 5 1949 ÷ 5 389 ÷ 5 77 ÷ 5 15 ÷ 5 3÷5 = = = = = = = 9746 1949 389 77 15 3 0 References Examples r r r r r r r 2 1 4 4 2 0 3 819210 ÷ 8 1024 ÷ 8 128 ÷ 8 16 ÷ 8 2÷8 = = = = = 1024 128 16 2 0 r r r r r 0 0 0 0 2 819210 = 200008 4873210 = 30244125 200008 = 2.84 + 0.83 + 0.82 + 0.81 + 0.80 = 819210 Check: 3.56 + 0.55 + 2.54 + 4.53 + 4.52 + 1.51 + 2.50 = 4873210 11 12 Number Systems Binary Arithmetic References Number Systems Decimal to Binary — With Division Binary Arithmetic References Decimal to Binary — With Division 57310 ÷ 2 286 ÷ 2 143 ÷ 2 121 ÷ 2 60 ÷ 2 30 ÷ 2 15 ÷ 2 7÷2 3÷2 1÷2 Repeated division of quotient by 2 as in previous example. Repeat until quotient is 0. Output remainders in reverse sequence to the one in which they were generated: LIFO! = = = = = = = = = = 286 143 121 60 30 15 7 3 1 0 r r r r r r r r r r 1 0 1 1 0 0 1 1 1 1 57310 = 11110011012 Check: 1.29 +1.28 +1.26 +1.26 +0.25 +0.24 +1.23 +1.22 +0.21 +1.20 = 57310 13 14 Number Systems Binary Arithmetic References Number Systems Decimal to Binary — Without Division Decimal to Binary — Without Division Convert 17410 to binary: 174 − 128 = 46 −→ 1 in 27 place 64 > 46 −→ 0 in 26 place 46 − 32 = 14 −→ 1 in 25 place 16 > 14 −→ 0 in 24 place 14 − 8 = 6 −→ 1 in 23 place 6−4=2 −→ 1 in 22 place 2−2=0 −→ 1 in 21 place 1>0 −→ 0 in 20 place = 101011102 Find largest power of 2 smaller than current number. Subtract this from the number and repeat on the difference Usually not practical for larger numbers 15 Binary Arithmetic 16 References Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Octal Representation Binary to Octal 08 0002 Can interpret octal number 70538 as: 7 × 83 + 0 × 82 + 5 × 81 + 3 × 80 The base or radix is 8 Digits 0, 1, 2, 3, 4, 5, 6, 7 18 0012 28 0102 38 0112 48 1002 58 1012 68 1102 78 1112 Idea: Collect bits into groups of three starting from right to left “pad” out left-hand side with 0’s if necessary Place values: · · · 512 ··· 83 Write number as 70538 (= 362710 ) 64 82 8 81 Convert each group of three bits into its equivalent octal representation (given in table above) 1 80 Example, 10111110001010012 011 3 001 1 17 111 7 000 0 101 5 0012 18 18 Number Systems Binary Arithmetic References Number Systems Octal to Binary Binary Arithmetic References Hexadecimal Representation Can interpret hexadecimal number 3AF1 as: 3 × 163 + 10 × 162 + 15 × 161 + 1 × 160 Reverse the previous process Convert each octal digit into equivalent 3-bit binary representation The base or radix is 16 Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example, 270158 Place values: 2 010 7 111 0 000 1 001 · · · 4096 ··· 163 Write number as 3AF116 (= 1508910 ) 58 1012 19 256 162 16 161 1 160 20 Number Systems Binary Arithmetic References Number Systems Binary to Hexadecimal 016 00002 816 10002 116 00012 916 10012 216 00102 A16 10102 316 00112 B16 10112 416 01002 C16 11002 516 01012 D16 11012 Binary Arithmetic References Binary to Hexadecimal 616 01102 E16 11102 716 01112 F16 11112 10111110001010012 Idea: Collect bits into groups of four starting from right to left Convert each group of four bits into its equivalent hexadecimal representation (given in table above) Example, 10111110001010012 21 1011 B 1110 E 0010 2 10012 916 0010 2 1111 F 0101 5 1100 C16 101111010111002 “pad” out left-hand side with 0’s if necessary 22 Number Systems Binary Arithmetic References Number Systems Hexadecimal to Binary Binary Arithmetic carry Convert each hexadecimal digit into equivalent 4-bit binary representation 1 1 2097 + 5 Example, AD516 A 1010 D 1101 References Decimal Arithmetic — Addition Reverse the previous process 2102 5 01012 Important principle of “sum” and “carry”. 23 References 24 Addend Augend Sum Number Systems Binary Arithmetic References Number Systems Binary Arithmetic — Addition Binary Arithmetic References Binary Arithmetic — Addition Similar idea: “sum” and “carry” 1010 + 1210 = 2210 Four cases to consider Addend Augend Sum Carry 0 0 — 0 0 0 1 — 1 0 1 0 — 1 0 1 1 — 0 1 1 1 1 0 0 1 1 1 0 1 0 0 0 + 26 25 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic References Multiplying Binary Numbers by 2 Dividing Binary Numbers by 2 Consider decimal multiplication: 10010 × 1010 = 100010 ; shift digits one place to the left introducing 0 to the right Consider decimal division: 10010 ÷ 1010 = 1010 ; shift digits one place to the right removing rightmost digit Similar idea applies to binary numbers Similar idea applies to binary numbers Binary multiplication: 1002 × 210 = 10002 (i.e., 410 × 210 = 810 ); shift digits one place to the left introducing 0 to the right Binary division: 1002 ÷ 210 = 102 (i.e., 410 ÷ 210 = 210 ); shift digits one place to the right removing rightmost bit C’s bitwise shift: >> shift right C’s bitwise shift: << shift left int x = 12; x = x >> 1; printf("%d\n", x); int x = 12; x = x << 1; printf("%d\n", x); 27 28 Number Systems Binary Arithmetic References Number Systems Bitwise Shift Binary Arithmetic References Negative Binary Numbers Several possible representations Signed magnitude One’s complement Two’s complement (used in C for integers) Example: Bitwise shift by two places (i.e., ÷4 and ×4) 0 0 0 0 1 1 0 0 ( = 12) 0 0 0 0 0 0 1 1 >> 2 (= 3) 0 0 1 1 0 0 0 0 << 2 (= 48) What happens when we bitwise shift signed quantities? Most significant bit (MSB; left-most bit) determines whether number is positive or negative Most significant bit (MSB) 1 29 1 Least significant bit (LSB) 0 1 0 0 1 0 30 Number Systems Binary Arithmetic References Number Systems Signed Magnitude Binary Arithmetic References Signed Magnitude Consider 4-bit binary 00002 00012 00102 00112 01002 01012 01102 01112 Utilise MSB as a “sign” bit: 0 = positive; 1 = negative Remaining bits express number in binary as usual Example 5310 = 001101012 −5310 = 101101012 How many numbers can be represented with this scheme? 010 110 210 310 410 510 610 710 10002 10012 10102 10112 11002 11012 11102 11112 010 −110 −210 −310 −410 −510 −610 −710 Can express −(2n−1 − 1). .2n−1 − 1, i.e., 2n − 1 numbers. 31 32 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic One’s Complement References One’s Complement Consider 4-bit binary 00002 00012 00102 00112 01002 01012 01102 01112 Invert 0’s and 1’s MSB indicates sign: 0 = positive; 1 = negative Example 5310 = 001101012 −5310 = 110010102 How many numbers can be represented with this scheme? 010 110 210 310 410 510 610 710 −710 −610 −510 −410 −310 −210 −110 010 10002 10012 10102 10112 11002 11012 11102 11112 Can express −(2n−1 − 1) . . 2n−1 − 1, i.e., 2n − 1 numbers. 33 34 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Two’s Complement Two’s Complement Need more efficient representation Positive numbers as usual Negative numbers: −532 Compute one’s complement representation Add 1 MSB indicates sign: 0 = positive; 1 = negative Example 5310 = 001101012 −5310 = 110010112 N = −d 0 1 0 1 1 0 1 0 0 1 1 0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 1 0 1 0 0 0 1 0 1 1 one’s complement +1 two’s complement 0 1 1 0 one’s complement +1 two’s complement −482 Theorem The representation of a numer N as n-bit two’s complement is a vector (d (i) )0≤i<n of binary digits satisfying: (n−1) References n−1 ×2 + n−2 X (d (i) i ×2 ) i=0 35 36 Number Systems Binary Arithmetic References Number Systems Two’s Complement Consider 4-bit binary 00002 00012 00102 00112 01002 01012 01102 01112 010 110 210 310 410 510 610 710 10002 10012 10102 10112 11002 11012 11102 11112 Binary Arithmetic References A Shortcut to Two’s Complements Scan the original binary from right to left, leaving all least significant zeros and the first one unchanged and then complement the remaining digits to the left of the first one. [Bri03] −810 −710 −610 −510 −410 −310 −210 −110 Example 1001011001100000 ⇒ 1001011001100000 ⇒ 0110100110100000 Can express −(2n−1 ). .2n−1 − 1, i.e., 2n numbers. Exercise: verify the table above 38 37 Number Systems Binary Arithmetic References Number Systems Two’s Complement Advantages Binary Arithmetic Two’s Complement Advantages cont. Extending, say, from int to long merely requires to pad with the MSB. This obviously works for non-negative numbers. For negative numbers observe that, when extending from n bit two’s complement to n + k bits, we replace the summand −(2n−1 ) by More efficient than signed magnitude and one’s complement Zero represented once only − (2n+k−1 ) + Used in C to represent negative integers Can be used to implement subtraction via addition n+k−2 X (2i ) i=n−1 = −2 × (2n+k−2 ) + 2n+k−2 + Define subtraction x − y to be x + (−y ) That is, convert −y to two’s complement form and add it to x as in unsigned binary. This works neither with signed magnitudes nor ones’ complements. n+k−3 X i=n−1 = −2n+k−2 + n+k−3 X i=n−1 .. . = −(2n−1 ) 39 References 40 (2i ) (2i ) Number Systems Binary Arithmetic References Number Systems Binary Subtraction via Addition and Two’s Complement 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 0 1 0 0 0 1 0 1 1 0 1 Recall that in two’s complement we can represent numbers in the range −(2n−1 ) to (2n−1 − 1) If we consider two positive binary numbers x and y where x + y > 2n−1 − 1 we can see that this sum will result in a negative number (the MSB is 1) positive overflow If we consider two negative binary numbers −x and −y where x + y > 2n−1 we can see that this sum will result in a positive number (the MSB is 0) negative overflow Can use XOR gate in hardware to determine overflow condition 42 41 Number Systems Binary Arithmetic References Number Systems Positive Overflow in Two’s Complement Addition of 4-bit positive 0 0 0 Binary Arithmetic References Negative Overflow in Two’s Complement numbers without overflow x x x x x x + x x x Addition of 4-bit negative numbers 1 x x 1 x x 1 1 x x Carry into MSB must have been 0; carry out of MSB is 0 Addition of 4-bit positive 0 0 1 without overflow x x + x Carry into MSB must have been 1; carry out of MSB is 1 numbers with overflow x x x x x x + x x x Addition of 4-bit negative numbers 1 x x 1 x x 1 0 x x Carry into MSB must have been 1; carry out of MSB is 0 with overflow x x + x Carry into MSB must have been 0; carry out of MSB is 1 carry in 6= carry out means overflow has occurred carry in 6= carry out means overflow has occurred 43 44 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic References Converting Fractions to Base x Fractions In the same way that we use a decimal point (.) to represent fractional quantities for decimal numbers, we use the radix point to represent fractional quantities in any base Convert integer component as usual To convert fractional component use “separate and multiply” technique Consider fractional component of abcd .pqrsx For example, abcd .pqrsx (note: base x) represents ax 3 + bx 2 + cx 1 + dx 0 + px −1 + qx −2 + rx −3 + sx −4 px −1 + qx −2 + rx −3 + sx −4 Multiplying by x gives: p + qx −1 + rx −2 + sx −3 Multiplying remaining fractional component by x gives: q + rx −1 + sx −2 Numbers to the left of radix point represent integer component Numbers to the right of radix point represent fractional component Repeat until fractional part exhausted or you have sufficient digits (Note: process is not guaranteed to terminate) Polynomial above can be evaluated to determine equivalent decimal representation. 46 45 Number Systems Binary Arithmetic References Number Systems Example Binary Arithmetic References Example Convert 23.312510 to base 8 Convert 23.312510 to base 2 Integer component Integer component 2310 ÷ 2 11 ÷ 2 5÷2 2÷2 1÷2 2310 ÷ 8 = 2 r 7 2÷8 = 0 r 2 Fractional component .312510 × 8 = 2.5 .510 × 8 = 4.0 Therefore, 23.312510 = 27.248 47 References Overflow in Two’s Complement Consider 8-bit binary representation 3510 − 1510 = 2010 0 0 1 1 0 0 −5310 − 4810 = −10110 1 1 1 1 1 0 Binary Arithmetic 48 = = = = = 11 5 2 1 0 r r r r r 1 1 1 0 1 Number Systems Binary Arithmetic References Number Systems Binary Arithmetic Example continued Floating Point Numbers Beyond the scope of this course Broadly speaking, floating point numbers are expressed as Fractional component .312510 × 2 = .625 × 2 = .25 × 2 = .5 × 2 = Therefore, 23.312510 = 10111.01018 ±d0 .d1 d2 · · · dp−1 × β exp 0.625 1.25 0.5 1.0 where d0 .d1 d2 · · · dp−1 is the significand (or mantissa) β is the base (so the di are expressed in this base) exp is the exponent Idea is that of scientific notation International Standard IEEE754 (1985) β = 2; p = 24 (single precision); p = 53 (double precision) 49 50 Number Systems Binary Arithmetic References References Robert Britton. MIPS Assembly Language Programming. Prentice Hall, 2003. Sample chapters available at ftp://ftp.prenhall.com/pub/ esm/sample_chapters/cs/britton/index.html. 51 References