Lecture 3: Logic Bryan Burlingame 06 Sept 2017.

Slides:



Advertisements
Similar presentations
Java Planning our Programs Flowcharts Arithmetic Operators.
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Professor Jennifer Rexford COS 217
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Integer Conversion Between Decimal and Binary Bases Conversion of decimal to binary more complicated Task accomplished by –Repeated division of decimal.
Lecture 2: Operations Bryan Burlingame 9Sept2015.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Computer Organization and Assembly Language Bitwise Operators.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Introducing programming March 24. Program structure Statements to establish the start of the program Variable declaration Program statements (block statements)
Lecture 1: Course Introduction Overview of Computers and their Applications in MAE B Burlingame 2 September, 2015 Heavily based on work by Dr. Buff Furman,
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Lecture 2: Logic Bryan Burlingame 10 Feb Ref: xkcd:
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Learning Objectives 3.3.1f - Describe the nature and uses of floating point form 3.3.1h - Convert a real number to floating point form Learn how to normalise.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 2: Logic Bryan Burlingame 31 Aug 2016.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Chapter 4 Operations on Bits.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 2 - Introduction to C Programming
Instructor: David Ferry
Chapter 2 - Introduction to C Programming
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Data Structures Mohammed Thajeel To the second year students
Week 6: Microcontrollers II
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture 2 Python Programming & Data Types
Chapter 2 - Introduction to C Programming
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Chapter 2 - Introduction to C Programming
C Programming Getting started Variables Basic C operators Conditionals
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Chapter 2 - Introduction to C Programming
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
OPERATORS in C Programming
Presentation transcript:

Lecture 3: Logic Bryan Burlingame 06 Sept 2017

Ref: xkcd: www.xkcd.com

Announcements Homework posted Homework 1 & 2 due next week Lab kits will be available before labs Read chapters 4 & 5 (only through page 83, i.e. the “if” statement) in the text

Announcements Visual Studio 2017 is different File -> New Project Select “Windows Desktop Wizard” (ok) Uncheck “Precompiled Headers” Check “Empty Project”

The Plan for Today Binary mathematics Discuss Boolean logic Briefly introduce flowcharts For flowcharts, use a proper graphics program. Visio is the standard, and is in labs Gliffy is online, free, and good enough (http://www.gliffy.com) Don’t use Powerpoint or Word. They are time wasters and SUPER ugly.

Flowcharts - 1 Flowcharts A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program Symbol Name/Meaning Meaning Process – Any type of internal operation: data transformation, data movement, logic operation, etc. Connector – connects sections of the flowchart, so that the diagram can maintain a smooth, linear flow Input/Output – input or output of data Terminal – indicates start or end of the program or algorithm Decision – evaluates a condition or statement and branches depending on whether the evaluation is true or false Flow lines – arrows that indicate the direction of the progression of the program

Flow control These Boolean operations are used along with flow control (or branching) statements to control the flow of a program Decisions True False

Recall Computers natively work with on/off, 0/1, binary numbers Binary is similar to decimal Binary: base 2 Decimal: base 10 2,54610 = 2 x 103 + 5 x 102 + 4 x 101 + 6 x 100 10112 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 1110

Bitshifts (<< Left Shift, >> Right Shift) A bit shift shifts bits in a direction Left shift shifts bits to the left and simulates a multiply of 2 Right shift shifts bits to the right and simulates an integer division of 2 Bits outside the range are lost 23 << 3 = 184 (left shift by 3) 0001 0111 shifted = 1011 1000 23 >> 2 = 5 (right shift by 2) 0000 0101 = 5 (note the last two bits are lost)

Bitwise arithmetic Mimics digital hardware Based on Boolean logic Operates on each bit within a number (i.e. operates bitwise) Many, many hardware peripherals take advantage of bitwise operations Implemented directly in hardware Very fast

True and False Generally, false is zero and true is not-zero Usually, all comparisons which are true generate a 1 (23 > 4) = 1 This comes up a lot!

Binary (Bitwise) And - & 1 X 194 & 225 = 192 Binary And is commonly used with “bitmasks” A binary & used with a bitmask can ensure that a given bit is turned “off” & is shift + 7 1100 0010 (194) & 1110 0001 (225) -------------------------------- 1100 0000 (192)

Binary (Bitwise) Or - | Y | 1 X 194 | 225 = 227 1 X 194 | 225 = 227 Binary OR, is, also, commonly used with “bitmasks” A binary | used with a bitmask can ensure that a given bit is turned “on” | is shift + \ 1100 0010 (194) | 1110 0001 (225) -------------------------------- 1110 0011 (227)

Binary (Bitwise) Xor - ⊕ 1 X 194 ^ 225 = 35 Xors are commonly used to switch the values of selected bits or to test for inequivalence C uses ^ for Xor ^ is shift + 6 1100 0010 (194) ⊕ 1110 0001 (225) -------------------------------- 0010 0011 (35)

Binary (Bitwise) Not (complement) - ~ 1 X ~ 194 = 61 Complements simply flip the bits (turn 0’s into 1’s and 1’s into 0’s) C uses ~ for complement ~ is shift + ` (next to 1) 1100 0010 (194) ~ ---------------------------- 0011 1101 (61)

Order of operations – C & Matlab Operators * / % Multiplication, division, modulo (integer) division + - Addition, subtraction << >> Left bitshift, right bitshift & Bitwise and ^ Bitwise xor | Bitwise or

Modulo Division % Not bitwise, but a necessity due to hardware Recall: computers only natively work with integers To handle fractions, additional hardware is required Need a way to deal with fractions in an integer only world Solution: modulo division

Modulo Division % Modulo division returns the remainder of a from a division 32 / 5 = 6.4 = 6 2/5 = 6 remainder 2 32 % 5 = 2

Boolean Logic And && False True Exclusive Or (Xor) ^^ False True Or ||

Boolean Logic - Examples By convention, 0 is false. Nonzero is true. ( 6 < 14 ) && ( 9 > 5 ) 5 || (19 > 45) && (57 < 108) (My name is Bryan) && (This is ME30) (name == “Bryan”) && (class == “ME30”) Note: this is conceptual. String comparisons don’t use this operator in C ((16 + 45) == 61) ^^ ((49) == (7 * 7)) 42

Assignment Operators Recall a basic assignment float x = 5.0; // stores 5.0 in memory location x x = x + 12.0; // retrieves 5.0, adds 12, stores 17 in x (note: this is not algebra) There are number off short-form assignment operators for the common retrieve, operate, store form

Assignment operators x += 12.0; // x = x + 12.0 x -= y; // x = x – y x *= z + 2; // x = x * (z + 2) (note the implied parenthesis) x /= z + 2; // x = x / (z + 2) Works for all operators we’ve seen +, -, *, /, |, &, ^, %, <<, >>

Increment / Decrement x += 1; is also very common (increment) Given: x++; or ++x; // x = x + 1 Similar function but different return values Given: int x = 5; printf( “%i\n”, ++x ); printf( “%i\n”, x++ ); What prints?

Increment / Decrement 6 Prints. ++x adds 1 and then returns the new value. x++ returns the original value and then adds 1. ++x saves one operations. Sometimes mildly faster There are also --x and x-- which work the same, but with subtraction (i.e. decrement)

References Nicholas, P. (1994). Digital Design. Minneapolis/St. Paul: West Publishing Company