Lecture Notes 15 Number systems and logic CSS 501 - Data Structures and Object-Oriented Programming Professor Clark F. Olson Number systems The use of alternative number systems is important in computer science. Computers don t use base 10 when they process numbers and code; they use binary bits, which can only be 0 or 1, instead of 0 through 10 in the decimal system that we are used to. To understand how other number systems work, let s first look at how the decimal system works. If I write 1234, we understand that this means one thousand, two hundred, and thirty-four. Why? This number can be thought of as a sequence of digits. For convenience, we will start at the rightmost digit, so 1234 can be thought of as the sequence s 0 =4, s 1 =3, s 2 =2, s 3 =1. This is converted into a number by taking each digit and multiplying it by a power of 10. If n is the number of digits in our number, then we can compute the number from the sequence s using: n 1 s i i 0 10 i 4 10 0 3 10 1 2 10 2 1 10 3 1234 Working in another number system is the same, except we use a different base for the exponent. In binary, we would write: So, 101101 (base 2) is 1 + 1*4 + 1*8 + 1*32 = 45. How do we write 21 in binary? First, we have to find the largest digit. We find the largest power of 2 that is smaller than (or equal to) the number. 2 4 = 16 is smaller than 21, but 2 5 is not. This means that our number with have 5 digits. In binary, the digit can only be 1 or 0, so in this way it is a little easier than decimal (or other number systems). The first digit is, of course, 1. We then remove 16 from the number to get 5 and go to the next digit. The next digit is for 8s. There is no 8 left in the number, so the next digit is 0. At the 4s spot, we have a 1, since 5 is larger than 4, leaving 1 after removing the four. There is a 0 at the 2s positions and a 1 at the final position, so the result is: 101012. Adding in binary is similar to adding in decimal. We carry a digit if we get a result greater than 1 (instead of greater than 9). 10011011 +1011011 11110110 Hexadecimal (base 16) is also commonly used, since it can abbreviate binary numbers. To write a number in hexadecimal, we use: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Converting from binary to hexadecimal is easy, because we can just group each four digits (starting from the right and convert it into a hex value): 11 0101 1101 0010 = 35D2 Of course, we can add and subtract in hexadecimal. 84F 3C9 + 42EA - 4D 4B39 37C n 1 s i i 0 2 i
A useful method to convert to from decimal to any number system is to repeated divide the number by the base of the new number system. The remainders form the digits of the new number from right to left. For example, what is 32 in base 3? 1012 3 = 32 10. 32 / 3 = 10 with remainder 2 10 / 3 = 3 with remainder 1 3 / 3 = 1 with remainder 0 1 / 3 = 0 with remainder 1 We can write code to convert from base 10 to any base between 2 and 36 as follows: string convertbase(int n, int base) { string result = ""; while (n > 0) { int digit = n % base; n = n / base; if (digit < 10) { result = char('0' + digit) + result; else { result = char('a' + digit - 10) + result; return result; Logic In the rest of this lecture, we are going to talk mostly about logic and proofs. We won t discuss programming or code at all. This will be the case in several other lectures this quarter, also. A large part of this class has to do with the mathematical foundations that are used in computer science. Logic is extremely important. Every time you use an if-then statement, you are using logic to determine the outcome of your program. However, it goes much deeper than this. Logic is the study of reasoning - of determining whether statements are true or false. If you don t have a good foundation in logic and logical reasoning, then it is difficult to reason effectively about your programs. Logic is also used to prove that programs do what you claim that they do. If you give someone a program to accomplish some task, what reason do they have to believe that the program works correctly? Even if they verify that, it works correctly on 1000 different inputs, that doesn t mean it works correctly on all inputs. You have to use logic to demonstrate that the program is always correct. Propositions Logic deals with statements that are either true or false. These statements are called propositions. They do not need to be true to reason about them. The following are propositions: Starbucks sells coffee in Seattle. Slayer plays easy listening music. The Seahawks will win the Super Bowl this year. (True or false, we just don t know which!) Are these statements true or false? Questions and commands are not propositions: Do I own any cats? Finish your programming assignment. For the most part, we won t pay much attention to what the particular propositions mean. We will give them abbreviated names (lower-case letters) to represent them and reason about them using their abbreviations. So, a could represent the proposition My car is black and b could represent the proposition My car is a Ferrari. To write the conjunction (logical and) of two propositions we write: a^b.
This is true if and only if both a and b are true. We write the disjunction (logical or) of two propositions as: a b. This is true if either a or b is true. For any particular operator, such as conjunction or disjunction, we can define it using a truth table that says what the value is for any possible combination of inputs: a b a^b a b 0 0 0 0 (I will use 0 for false and 1 for true in these notes.) 1 0 0 1 0 1 0 1 1 1 1 1 We can also take the negation of a statement. The negation of a is written a. Negation has the following truth table: a a 0 1 1 0 Let s consider the following propositions: a: A New Hope is Star Wars episode IV. b: Hoth is a desert planet. c: Darth Vader is Princess Leia s father. How would we represent the statement: `A New Hope is not Star Wars episode IV or Darth Vader is Princess Leia s father, and Hoth is a desert planet. ( a c)^b Is this a true statement? Conditional propositions are like if-then statements in programming. If a and b are propositions, then: if a then b is also a proposition (a conditional proposition). In logic, this is usually written a b, sometimes this is pronounced a implies b. The proposition a b is true if b is true whenever a is true. The truth table is: a b a b b a a b (biconditional) 0 0 1 1 1 0 1 1 1 0 1 0 0 0 0 1 1 1 1 1 Note that the proposition b a is logically equivalent to a b, they both have the same outcome, no matter what a and b are. Some other propositions that are logically equivalent: (a b) a^ b (a^b) a b (These two are known as De Morgan s laws) (a b) a^ b a b (a b)^(b a) Let a = This class starts at 6:00pm and b = My name is Clark. Is a b true? What if a = This class starts at 8:00pm? The order of precedence of the operators is:, ^,,
Quantifiers So far we are only able to write propositions without any variables (a and b were abbreviations for known statements). x=2y+1 is not a proposition (if x and y are unknown variables) since it is not either true or false (it depends on x and y). However, we would like to be able to create propositions that have variables in them. To do this, we use quantifiers. Let P(x) be a statement involving the variable x and D be a set (the domain of x). P is a propositional function if, for every x in D, P(x) is a proposition. Given a propositional function, we can create a proposition using either of two quantifiers: Existential quantifier: x P(x) There exists x such that P(x) is true. Universal quantifier: x P(x) For all x, P(x) is true. Using the set of integers as our domain D, some propositions using these quantifiers are: x, x = 5 y, y = y + 2 x, 10x > x x, x² 0 The first and last are true, the middle two are false. We can build up propositions with multiple variables. Example: x y z x + y + z = 0 To determine if propositions like this are true or false, you play a logic game. You play against an adversary that tries to make the statement false, while you try to make the statement true. Starting with the quantifier that is the farthest to the left in the statement, either you or your adversary picks a value for the variable. If the quantifier is universal (for all), then your adversary picks the value. If the quantifier is existential (there exists), you get to pick the value. If you opponent is able to choose values for the universally quantified variables, such that you cannot choose values for the others that make the statement true, you lose (the statement is false). If you can choose values that make the statement true, no matter what values your opponent chooses, then you win (the statement is true). This is sometimes called an adversary argument or adversary proof. If you are my adversary, you cannot choose values for x and y such that I cannot make the statement above true. Not matter what you choose, I can pick z = x y and the proposition will be true. Proofs A theorem is a proposition that has been proven to be true. An example is the proposition from the previous section: x y z, x + y + z = 0 A proof is an argument that establishes the truth of a proposition (or theorem). A common type of proposition looks like this: For all x1, x2,, xn, if p(x1, x2,, xn), then q(x1, x2,, xn) A direct proof of such a theorem assumes that p(x1, x2,, xn) is true and shows that q(x1, x2,, xn) is true. Let s prove that: For all x, y, if x = 2y, then (x + 1)² = 4y² + 4y + 1 Assuming that x = 2y, we can replace x with 2y in the second proposition, yielding: (2y + 1)² = 4y² + 4y + 1 An indirect proof, assumes that p is true and q is false and derives a contradiction. This also proves the theorem, since if p then q is equivalent to p q and also q p. Example: For all integers, x and y, if x + y > 0 then x > 0 or y > 0. Assume x + y > 0, x <= 0, y <= 0. The second two combined imply that x + y <= 0, which contradicts the first statement.
One way of reasoning about propositions is to construct arguments. In this context, argument has a very specific meaning. An argument is a proposition of the form: If p1 and p2 and and pn, then q. Note that an argument (like any other proposition) can be either true or false. Arguments of this form are commonly written like this: p 1 p 2 p n Δq This is the same as p1 ^ p2 ^ ^ pn q. One way to prove an argument is to use a truth table. A proof using a truth table is constructed by trying every possible combination of values for the variables in the argument. Let s try: a a b b a b a b a^( a b) (a^( a b)) b 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 1 1 1 1 1 This argument is proven true, since it is true for any possible combination of true and false for a and b. Practice problem: write a truth table for ( x ^ y) z. Resolution (optional) Another way of proving arguments is using a technique called resolution. The basic rule of resolution says: If p q and p r are both true, then q r is true. Formally, resolution can be written as follows: (p q)^( p r) (q r) Here is a truth table that proves this is true: p q r p (p q) ( p r) (p q)^( p r) (q r) ((p q)^( p r)) (q r) 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 So, ((p q)^( p r)) (q r) is true for every possible combination of p, q, and r. We can use this rule to prove various arguments. Here is a simple example: a b a c Δb c This follows directly from the resolution rule. Another example, with more clauses: a b b c a d Δc d (from these two we get a c) (from these we get c d)
We can also prove arguments that don t look like they fit the definition of resolution: a (this is the same as a false) a c (using resolution this yields c false, which is the same as c) Δc Finally, a (b^c) this is (a b)^(a c) a from this we get b Δb Practice problems (optional): Write the truth table for: (y^ z) (x ^ y) Convert 24315 to binary. (Hint: first convert it to decimal)