Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2: Logic Bryan Burlingame 10 Feb 2016. Ref: xkcd: www.xkcd.com.

Similar presentations


Presentation on theme: "Lecture 2: Logic Bryan Burlingame 10 Feb 2016. Ref: xkcd: www.xkcd.com."— Presentation transcript:

1 Lecture 2: Logic Bryan Burlingame 10 Feb 2016

2 Ref: xkcd: www.xkcd.com

3 Announcements Homework posted Homework 1 due next week E-mail: spring2016@me30.orgspring2016@me30.org Lab 1 due in Lab

4 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)http://www.gliffy.com Don’t use Powerpoint or Word. They are time wasters and SUPER ugly.

5 Recall binary Base 2 number system Mimics the actual hardware computers are built upon Two values, 0 and 1 01111000 2 to decimal 2 6 + 2 5 + 2 4 + 2 3 = 120

6 Decimal to binary Many ways, this is how I do it 120  120 < 128, 0 in 7 th position  120 > 64, 1 in 6 th position 120 – 64 = 56  56 > 32, 1 in 5 th position 56 – 32 = 24  24 > 16, 1 in 4 th position 24 – 16 = 8  8 = 8, 1 in 3 rd position 8 – 8 = 0  Once we hit zero, all other values are zero  0111 1000 Why do I do it this way? It is quick when I don’t have a calculator. Your mileage may vary BinaryDecimal 11 102 1004 10008 1000016 10000032 100000064 10000000128

7 Bitshifts ( > 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)

8 Representations Decimal (default)Binary 1080’01101100 108d0110 1100b 108 10 0110 1100 2 108 dec0110 1100 bin Group by 3s with a comma Group by 4s with a space or an underbar

9 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

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

11 Binary (Bitwise) And - & &01 000 101 Y X 194 & 225 = 227 1100 0010 (194) & 1110 0001 (225) -------------------------------- 1100 0000 (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

12 Binary (Bitwise) Or - | |01 001 111 Y X 194 | 225 = 227 1100 0010 (194) | 1110 0001 (225) -------------------------------- 1110 0011 (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 + \

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

14 Binary (Bitwise) Not (complement) - ~ ~ 01 10 X 194 ^ 225 = 35 1100 0010 (194) ~ ---------------------------- 0011 1101 (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)

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

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

17 Boolean Logic - Examples ( 6 5 ) 5 || (19 > 45) && (57 < 108) (My name is Bryan) && (This is ME30)  (name == “Bryan”) && (class == “ME30”) ((16 + 45) == 61) ^^ ((49) == (7 * 7)) 42

18 Flowcharts - 1 Flowcharts  A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program SymbolName/MeaningSymbolMeaning 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

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

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


Download ppt "Lecture 2: Logic Bryan Burlingame 10 Feb 2016. Ref: xkcd: www.xkcd.com."

Similar presentations


Ads by Google