Lecture Notes 15 Number systems and logic CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Similar documents
CS Bootcamp Boolean Logic Autumn 2015 A B A B T T T T F F F T F F F F T T T T F T F T T F F F

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Bits, Words, and Integers

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Section 2.2: Introduction to the Logic of Quantified Statements

CSE 20 DISCRETE MATH. Fall

Lecture 6,

Fall Recursion and induction. Stephen Brookes. Lecture 4

AXIOMS FOR THE INTEGERS

Notes. Notes. Introduction. Notes. Propositional Functions. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry.

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

Chapter 6 Primitive types

To prove something about all Boolean expressions, we will need the following induction principle: Axiom 7.1 (Induction over Boolean expressions):

CSE 311: Foundations of Computing. Lecture 8: Predicate Logic Proofs

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 9 Normal Forms

Program Verification & Testing; Review of Propositional Logic

University of Illinois at Chicago Department of Computer Science. Final Examination. CS 151 Mathematical Foundations of Computer Science Fall 2012

CPSC 121: Models of Computation Assignment #4, due Thursday, March 16 th,2017at16:00

CMPSCI 250: Introduction to Computation. Lecture #7: Quantifiers and Languages 6 February 2012

Divisibility Rules and Their Explanations

Summary of Course Coverage

CPSC 121: Models of Computation. Module 6: Rewriting predicate logic statements

Chapter 3. Set Theory. 3.1 What is a Set?

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

The set consisting of all natural numbers that are in A and are in B is the set f1; 3; 5g;

Propositional Logic Formal Syntax and Semantics. Computability and Logic

Geometry Note-Sheet Overview

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

COMP combinational logic 1 Jan. 18, 2016

1 Definition of Reduction

CSE 20 DISCRETE MATH. Winter

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

2. BOOLEAN ALGEBRA 2.1 INTRODUCTION

Proofwriting Checklist

Solutions to Selected Exercises

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Introduction to Programming, Aug-Dec 2008

Reductions and Satisfiability

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

Bulgarian Math Olympiads with a Challenge Twist

Section 2.4: Arguments with Quantified Statements

for all x, the assertion P(x) is false. there exists x, for which the assertion P(x) is true.

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

The. Binary. Number System

Lecture 5. Logic I. Statement Logic

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Zero-Knowledge Proofs. Zero-Knowledge Proofs. Slides from a Talk by Eric Postpischil

The type of all data used in a C++ program must be specified

CS & IT Conversions. Magnitude 10,000 1,

Chapter 1 Operations With Numbers

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Chomp the Graph. Research. treet. road. Sam Magura, Vitchyr Pong, Elliot Cartee, Kevin Valakuzhy. Scientific

Introduction to Boolean Algebra

Maximal Monochromatic Geodesics in an Antipodal Coloring of Hypercube

Practice Problems: All Computer Science majors are people. Some computer science majors are logical thinkers. Some people are logical thinkers.

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Homework 1 CS 1050 A Due:

Introduction to Boolean Algebra

Predicate Logic CHAPTER What This Chapter Is About

Propositional Calculus. Math Foundations of Computer Science

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

CS103 Spring 2018 Mathematical Vocabulary

Chapter 1.3 Quantifiers, Predicates, and Validity. Reading: 1.3 Next Class: 1.4. Motivation

(Refer Slide Time 6:48)

Interpretations and Models. Chapter Axiomatic Systems and Incidence Geometry

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

4.1 Review - the DPLL procedure

Answer Key #1 Phil 414 JL Shaheen Fall 2010

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

4&5 Binary Operations and Relations. The Integers. (part I)

LECTURE 2 An Introduction to Boolean Algebra

Goals of this Lecture

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

BOOLEAN ALGEBRA AND CIRCUITS

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

The Satisfiability Problem [HMU06,Chp.10b] Satisfiability (SAT) Problem Cook s Theorem: An NP-Complete Problem Restricted SAT: CSAT, k-sat, 3SAT

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

Data Representation 1

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Chapter 2: Number Systems

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

Mathematical Logic Part Two

BINARY SYSTEM. Binary system is used in digital systems because it is:

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Lecture Notes on Induction and Recursion

Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Introduction to Informatics

Level ISA3: Information Representation

Transcription:

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)