Contents Number systems and conversion Binary arithmetic Representation of negative numbers Addition of two s complement numbers Addition of one s complement numbers Binary s Readings Unit.~. UNIT NUMBER SYSTEMS & CONVERSIONS Iris Hui-Ru Jiang Spring Number Systems (/) Number Systems (/) Positional notation: Each digit is multiplied by an appropriate power of base depending on its position in the number The point separates the positive and negative powers of base e.g., decimal (base ) numbers 9.8 = 9x +x +x +x - +8x - (.--): powers of base A positive number N with base (radix) R (positive integer, R>): Examples: Decimal (base ) numbers 9.8 =9x +x +x +x - +8x - Binary (base ) numbers. =x +x +x +x +x - +x - =. Octal (base 8) numbers. 8 =x8 +x8 +x8 +x8 - =. Hexadecimal (base ) numbers N = (a a a a a.a - a - a - ) R = a xr +a xr +a xr +a xr +a xr +a - xr - +a - xr - +a - xr - Base is also called radix Base is indicated as subscript AF =x +x +x = Q: Why do people use the decimal number system?
Common Number Systems Conversion of a Decimal Integer Name Decimal Binary Octal Hexadecimal Radix 8 Digits First positive integers,,,,,,,,8,9 8 9,,,,,,,,,,,,,,,, 8,9,A,B,C,D,E,F 8 9 A B C D E F Convert a decimal integer to base R using division: N = (a n a n-...a a a ) R = a n R n + a n- R n- +... + a R + a R + a N/R = a n R n- + a n- R n- +... + a R + a = Q...remainder a Q /R = a n R n- + a n- R n- +... + a R + a = Q... remainder a Q /R = a n R n- + a n- R n- +... + a = Q...remainder a Continued until we finally obtain.remainder a n e.g., convert to binary remainder = = a (LSB) remainder = = a remainder = = a = remainder = = a remainder = = a remainder = = a (MSB) Conversion of a Decimal Fraction (/) Conversion of a Decimal Fraction (/) Convert a decimal fraction to base R using multiplication: F = (.a - a - a -...a -m ) R = a - R - + a - R - + a - R - +... + a -m R -m FR = a - + a - R - + a - R - +... + a -m R -m+ = a - + F F R = a - + a - R - +... + a -m R -m+ = a - + F F R = a - +... + a -m R -m+ integer = a - + F fraction Continued until we finally obtain a sufficient number of digits e.g., convert. to binary. (). (a - =, MSB) (). (a - =). =. (). (a - =, LSB) 8 Sometimes, the result is a repeating fraction e.g., convert. to binary. (). ().8 (). (). Start repeating! ().. was previously obtained. =. ().8
Conversion between Two Bases (/) Conversion between Two Bases (/) 9 Convert between two bases R and R other than decimal Base R base base R e.g., convert. to base. = x +x +x +x - =. integer fraction. x (). rem. x rem. (). x (). x (). Repeat!. Convert between binary and octal/hexadecimal by inspection. Start at the binary point. Divide bits into groups of three/four, adding s if necessary. Replace each group by an octal/hexadecimal digit. And vice versa Binary octal. =. =. 8 adding s Binary hexadecimal. =. =D.C D C integer fraction. =. =. Binary Arithmetic -- Addition Binary Arithmetic Addition table: + = + = + = + = (and carry to the next column) e.g., add and in binary = = = carries
Binary Arithmetic -- Subtraction Binary Arithmetic -- Multiplication Subtraction table: = = = = (and borrow from the next column) Borrow from the next column have - at the next column = obtain R at the current column e.g., subtract 9 from 9 in binary 9 = 9 = = borrow Multiplication table: x = x = x = x = e.g., multiply by in binary = multiplicand x = multiplier copy of multiplicand if = Binary Arithmetic -- Division 9 AMC8 Is similar to (easier than) decimal division e.g., divide by in binary = quotient divisor = = dividend = remainder A, B, CD. AB AB + CA -CA, DA A A, B, C, D? Remark: decimal
9 AMC8 (Unit test) A, B, CD. AB AB + CA -CA, DA A 8 Representation of Negative Numbers A, B, C, D? Sol: A, B, C, D =,,, 9 B + A = A () => B = B + A = A, B A = A () => A = A C = () => A = C or A = C+ (, ) => C = => A + C = D = + = 9 Addition of two s complement numbers Addition of one s complement numbers Negative Numbers Signed Binary Integers 9 Sign and Magnitude: -bit sign + (n-)-bit magnitude S Magnitude word length: n sign bit: for positive, for negative e.g., sm = +, sm = - Is common for people but awkward for computers s complement: N_ = ( n ) N Complement +N bit-by-bit e.g., + =, _ = s complement: N* = n N = N_ + Complement +N bit-by-bit and then add Or complement all bits to the left of the rightmost e.g., + =, * = +N + + + + + + + + Positive integers (all systems) -N - - - - - - - - -8 Sign and magnitude ---- For word length n, there are n different permutations SM and N_: [+,, +, -,, -] (redundant -) N*: [+,, +,, -8] (- n- ~ + n- -) View the first bit as the sign bit: positive/negative / Word length: n = Negative integers s complement N* ---- s complement N_ ----
Warning: Unsigned or Signed Numbers Addition of Two s Complement Numbers For word length n, there are n different permutations Unsigned: [, n -] Signed SM and N_: [- n- +, + n- -] (redundant -) N*: [- n-, + n- -] e.g., what is?. Add just as if all numbers were positive. Discard the carry from the sign bit Why to discard the carry? (i.e., subtract n ). Add(-A, +B), B > A A* + B = ( n A) + B = n + (B A) n (B A). Add(-A, -B), A+B n- e.g., Add(+A, +B) A+B < n- + + + A* + B* = ( n A) + ( n B) = n + n (A + B) = n + (A + B)* n (A + B)* Add(+A, -B) B > A + - - Add(-A, +B) B > A - + + () Add(-A, -B) A+B n- - - - () Exception: Overflow in Two s Complement Addition of One s Complement Numbers An overflow means out of range of representation When the word length is n bits, the correct representation of a number (including sign) requires more than n bits How to detect overflow? Check sign! (+) + (+) (-) or (-) + (-) (+) e.g., Add(+A, +B) A+B n- + + Wrong answer! Overflow: + requires bits including sign Add(-A, -B) A+B > n- - - () Wrong answer! Overflow: - requires bits including sign End-around carry: Add the carry out back to the rightmost bit Why to take end-around carry? (i.e., subtract n & add ). Add(-A, +B), B > A A_ + B = ( n A) + B = n + (B A) n & + (B A). Add(-A, -B), A+B n- A_ + B_ = ( n A) + ( n B) = n + n (A + B) = n + (A + B)_ n & + (A + B)_ e.g., Add(+A, +B) A+B < n- + + + Add(+A, -B) B > A + - - Add(-A, +B) B > A - + + () Add(-A, -B) A+B n- - - - ()
Exception: Overflow in One s Complement How to detect overflow? Check sign! (+) + (+) (-) or (-) + (-) (+) e.g., Add(+A, +B) A+B n- Add(-A, -B) A+B n- Binary Codes + + Wrong answer! Overflow: + requires bits including sign - - () Wrong answer! Overflow: - requires bits including sign Decimal Digits to Binary Codes Decimal digit 8 9 Input/output interface generally uses decimal numbers How to represent decimal numbers using binary s? Choose elements from binary numbers of bits e.g., 9. BCD:. 8--- --- Excess- -out-of- Weighted Property a.k.a. BCD Weighted = BCD+ Only bits are one For error checking Gray Many options! Only bit changes as counting up/down For analog quantities 8 Warning: Conversion or Coding? Do NOT mix up conversion of a decimal number to a binary number with coding a decimal number with a binary e.g., (This is conversion) (This is coding)
Text to Binary Codes Homework for Unit 9 ASCII American Standard Code for Information Interchange English alphanumeric symbols bits Big- Traditional Chinese characters 8 bits Problems..... Homework # covers Units -- 9 printable characters are numbered to in ASCII Example: 9 AMC8 A, B, CD. AB AB + CA -CA, DA A A, B, C, D?