150 likes | 191 Views
Learn how computers work with 1s and 0s, converting binary to decimal, basic Boolean algebra, and more. Discover the power of voltages in representing information.
E N D
Fundamentals • What kind of data do computers work with? • Deep down inside, it’s all 1s and 0s • What can you do with 1s and 0s? • Boolean algebra operations • These operations map directly to hardware circuits CS231 Fundamentals
Volts 5 4 3 2 1 0 High Low Volts 5 4 3 2 1 0 High Medium Low Computers are binary devices • Computers use voltages to represent information • Voltages are usually limited to 2.5-5.0V to minimize power consumption • This is only enough to represent two discrete (digital) signals • Low and High • 0 and 1 • False and True • Why? • To account for noise • To prevent transitional errors • How can we use these two signals to represent numbers? Good! Bad! CS231 Fundamentals
Decimal review • Numbers consist of a bunch of digits, each with a weight • These weights are all powers of the base, which is 10. We can rewrite this: • To find the decimal value of a number, multiply each digit by its weight and sum the products. (1 x 102) + (6 x 101) + (2 x 100) + (3 x 10-1) + (7 x 10-2) + (5 x 10-3) = 162.375 CS231 Fundamentals
Converting binary to decimal • We can use the same trick to convert binary, or base 2, numbers to decimal. This time, the weights are powers of 2. • Example: 1101.01 in binary • The decimal value is: (1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) = 8 + 4 + 0 + 1 + 0 + 0.25 = 13.25 CS231 Fundamentals
Opposite: converting decimal to binary • To convert an integer, keep dividing by 2 until the quotient is 0. Collect the remainders in reverse order. • To convert a fraction, keep multiplying the fractional part by 2 until it becomes 0. Collect the integer parts (in forward order). • This may not terminate! • Example: 162.375: • So, 162.37510 = 10100010.0112 162 / 2 = 81 rem 0 81 / 2 = 40 rem 1 40 / 2 = 20 rem 0 20 / 2 = 10 rem 0 10 / 2 = 5 rem 0 5 / 2 = 2 rem 1 2 / 2 = 1 rem 0 1 / 2 = 0 rem 1 0.375 x 2 = 0.750 0.750 x 2 = 1.500 0.500 x 2 = 1.000 CS231 Fundamentals
Why does this work? • This works for converting from decimal to any base • Why? Think about converting 162.375 from decimal to decimal. • Each division “strips off” the rightmost digit (the remainder). The quotient represents the remaining digits in the number. • Similarly, each multiplication “strips off” the leftmost digit (the integer part). The fraction represents the remaining digits. 162 / 10 = 16 rem 2 16 / 10 = 1 rem 6 1 / 10 = 0 rem 1 0.375 x 10 = 3.750 0.750 x 10 = 7.500 0.500 x 10 = 5.000 CS231 Fundamentals
Base 8 and base 16 are useful too • Octal (base 8) digits range from 0 to 7. Since 8 = 23, one octal digit is equivalent to 3 binary digits. • Hexadecimal (base 16) digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Since 16 = 24, one hex digit is equivalent to 4 binary digits. • We typically use octal and hex as a shorthand for those long messy binary numbers. CS231 Fundamentals
Binary, octal, hexadecimal • Converting from octal (or hex) to binary is easy: just replace each octal (or hex) digit with the equivalent 3 (or 4) bit sequence • To convert from binary to octal (or hex): starting from the binary point, make groups of 3 (or 4) bits. Add 0s to the ends of the number if needed. Then, just convert each bit group to its corresponding octal (or hex) digit. 261.358 = 2 6 1 . 3 58 = 010 110 001 . 011 1012 261.3516 = 2 6 1 . 3 516 = 0010 0110 0001 . 0011 01012 These binary numbers are not equivalent! 10110100.0010112 = 010 110 100 . 001 0112 = 2 6 4 . 1 38 These numbers are equivalent! 10110100.0010112 = 1011 0100 . 0010 11002 = B 4 . 2 C16 CS231 Fundamentals
Functions • A computer takes input and produces output... just like a math function! • We can express math functions in two ways • We can represent binary functions in two ways too: • As a Boolean expression • As a truth table. It shows all possibilities for an expression. As an expression As a function table f(x,y) = 2x + y = x + x + y = 2(x + y/2) = ... CS231 Fundamentals
Basic Boolean operations • There are three basic operations. Each can be implemented in hardware using a primitive logic gate NOT (complement) on one input AND (product) of two inputs OR (sum) of two inputs Operation: Expression: xy x + y x’ Truth table: Logic gate: CS231 Fundamentals
Boolean expressions • We can use these basic operations to form more complex expressions and equations • Some terminology and notation • f is the name of the function • (x,y,z) are the input variables. Each variable represents a 1 or 0. Listing input variables is optional; instead we often write • A literal is an occurrence of an input variable or its complement. The function above has four literals: x, y’, z, and x’. • Without parentheses, the NOT operation has the highest precedence, followed by AND, and finally OR. Fully parenthesized, this function is f(x,y,z) = (x + y’)z + x’ f = (x + y’)z + x’ f(x,y,z) = (((x +(y’))z) + x’) CS231 Fundamentals
Larger circuits • We can build circuits for arbitrary expressions, using our basic gates f(x,y,z) = (x + y’)z + x’ (x + y’)z y’ (x + y’) x’ CS231 Fundamentals
Truth tables • We can make a truth table too, by evaluating the function for all possible inputs. • In general, a function with n inputs will have 2n possible inputs. • The inputs are typically listed in binary order. f(x,y,z) = (x + y’)z + x’ f(0,0,0) = (0 + 1)0 + 1 = 1 f(0,0,1) = (0 + 1)1 + 1 = 1 f(0,1,0) = (0 + 0)0 + 1 = 1 f(0,1,1) = (0 + 0)1 + 1 = 1 f(1,0,0) = (1 + 1)0 + 0 = 0 f(1,0,1) = (1 + 1)1 + 0 = 1 f(1,1,0) = (1 + 0)0 + 0 = 0 f(1,1,1) = (1 + 0)1 + 0 = 1 CS231 Fundamentals
Summary • Summary • To minimize analog voltage problems, computers are binary devices • That means we have to think in terms of base 2 • The basic operations on binary values include AND, OR, and NOT • These can be directly implemented in hardware (eventually we’ll show how to do useful stuff with these operations) • Expressions and truth tables are used to design and describe circuits • We’ve already seen some of the recurring themes of architecture: • We use 0 and 1 as abstractions for analog voltages • Gates are also an abstraction of the underlying technology • We showed how to represent numbers using just these two signals • Next time: • Using Boolean algebra to simplify expressions • This in turn will yield a simpler circuit CS231 Fundamentals