CS195, Class 9: Representing Integers Overview: * About binary representation * Representing unsigned integers * Computing with unsigned integers + Software + Hardware * Signed integers Administrivia: * Homework 4 is now ready! * Read more Gries for tomorrow. ---------------------------------------- Look! It's memory, nicely organized into chunks of eight bits (commonly called bytes). +----------+ | 01011001 | +----------+ | 01010000 | +----------+ | 11011111 | +----------+ | 01011101 | +----------+ | 11011000 | +----------+ | ... | Need formats for interpreting the 0's and 1's as * numbers + integers + reals * characters * parts of a string * instructions * images * ... What criteria would you use for designing or evaluating a format for a collection of values? * Tell each format and subtype apart * Efficiency + Coding (how easy is it to convert to the format) + Running (can we perform operations efficiently?) + Storage * Accuracy: Does it represent the data exactly or approximately * Scope: How big a subset does it cover * Avoid patented and copyrighted formats ---------------------------------------- First representation to study: unsigned integers Use an unfixed number of bits for each integer. N 1's in a row followed by a 0 represents N. * Inefficient in terms of storage * Managing memory will be unpleasant at best Binary Coded Decimals * Every four bits represents one decimal digit * Still ineficient in terms of storage Most natural representation of unsigned integers is "base two" Usually use a fixed number of bits for each uint How do you convert from binary to decimal in this format? * Scan from left to right, adding 2^appropriate_power when you see a 1. * Scan from left to right, adding 1 each time you see a 1 and doubling whenever you move one column to the right. 1011101 1 -> 2 -> 4+1=5 -> 10+1=11 -> 22+1=23 -> 46 -> 93 How do you convert from decimal to binary? * Start at "the right" * If it's odd, put a 1 in the "the current spot" * Otherwise, put 0 * Divide by two and move left How do you add? 11111 1 00011101 + 01110101 ---------- 10010010 Can we implement this algorithm in hardware? Rightmost column Two inputs: a0, b0 Two output: s0, c1 c1 = a0 and b0 s0 = (not (a0 = b0)) = a0 xor b0 All other columns Three inputs: ai, bi, ci Two output: si, c(i+1) si = (ai xor bi) xor ci ai bi ci c(i+1) 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 (((not ai) and bi) and ci) OR 1 0 0 0 1 0 1 1 (ai and (not bi) and ci) OR 1 1 0 1 (ai and bi and (not ci)) OR 1 1 1 1 (ai and bi and ci) ---------------------------------------- SUBTRACTION IS A PAIN! ---------------------------------------- How about multiplication of two eight bit numbers? 00010111 x 00000100 = 01011100 Repeating this technique over all N bits and adding gives a moderately efficient algorithm for multiplication What happens if you go "out of scope": Generate a too-big number? ---------------------------------------- Back to subtraction: Can we represent subtraction as "negate and add" First try: Signed magnitude. Leftmost bit gives sign. 0 is postive, 1 is negative * Negation is easy Second try: One's complement: To negate, flip all the bits * Negation is still easy * Can we add using the unsigned technique? 6 + -5 11 0110 + 1010 ------ 0000 -6 + 5 1001 + 0101 ------ 1110 = -1 Slight modification: If there's an overflow, add it at the right Clever extension: Two's complement: To negate, invert bits and add 1. Double negation gives the same value Negate 0: 11111111 add 1 => 00000000 Adding works correctly +1: 00000001 -1: 11111111