Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: Logic Bryan Burlingame 06 Sept 2017.

Similar presentations


Presentation on theme: "Lecture 3: Logic Bryan Burlingame 06 Sept 2017."— Presentation transcript:

1 Lecture 3: Logic Bryan Burlingame 06 Sept 2017

2 Ref: xkcd:

3 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

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

5 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 ( Don’t use Powerpoint or Word. They are time wasters and SUPER ugly.

6 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

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

8 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 x x x 100 10112 = 1 x x x x 20 = 1110

9 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) shifted = 23 >> 2 = 5 (right shift by 2) = 5 (note the last two bits are lost)

10 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

11 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!

12 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 (194) & (225) (192)

13 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 + \ (194) | (225) (227)

14 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 (194) ⊕ (225) (35)

15 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) (194) ~ (61)

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

17 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

18 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

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

20 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 (( ) == 61) ^^ ((49) == (7 * 7)) 42

21 Assignment Operators Recall a basic assignment
float x = 5.0; // stores 5.0 in memory location x x = x ; // 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

22 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 +, -, *, /, |, &, ^, %, <<, >>

23 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?

24 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)

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


Download ppt "Lecture 3: Logic Bryan Burlingame 06 Sept 2017."

Similar presentations


Ads by Google