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