Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2: Logic Bryan Burlingame 31 Aug 2016.

Similar presentations


Presentation on theme: "Lecture 2: Logic Bryan Burlingame 31 Aug 2016."— Presentation transcript:

1 Lecture 2: Logic Bryan Burlingame 31 Aug 2016

2 Ref: xkcd:

3 Announcements Homework posted Homework 1 due next week
Lab starts this week

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

5 Binary Representations
Computers only contain two values On/Off (High/Low, 0/1, etc.) Binary number only have two value 0/1 Base 2 numbering system A bit: A number in base 2 (binary digit) How computers do what they do? Humans are the translators The machine just presents information Think, what is a letter? Just a squiggle, humans give meaning

6 Binary Representations
Everything in a PC is encoded in binary Computers are finite, so are their representations RGB for colors 24 bit color (8 bits for red, 8 for green, 8 for blue) TIFF Music Midi – Instrument + note MP3 – Sound waves compressed Cruise Control Voltage level going to an actuator

7 Binary to Decimal Binary number only have two value 0/1
Base 2 numbering system A bit: A number in base 2 (binary digit) Decimal numbers have ten values 0 – 9 Base ten numbering system Think scientific notation 6.02E23 = 6 * * * 1021 = 8510= 1 * * * * * 20 Range? Largest 8 bit value: = 25510 Largest 16 bit value: =

8 Negative Values Recall: Sign bit 1’s Complement
The computer only deals in 0/1 Humans provide context Sign bit The first bit represents sign = -213, = 213 1’s Complement Flip the bits (change 1’s into 0’s) = - 213, = 213

9 Negative Values (Cont)
2’s Complement Take 1’s complement, add one Start (8410 in this example) Flip bits Add one (notice the carries)

10 Binary to Hexadecimal Hexadecimal (base 16) is a convenient numbering system 0 – 9, A – F (A = 1010, B = 1110, etc Observe 24 = 16, allows four bits (a nyble) to be grouped 11002 = 1210 = 0xC16

11 Binary to Hexadecimal Convert Binary to Hex b

12 Binary to Hexadecimal Convert Binary to Hex
b (grouping 4 bits) d C F h C39Fh 12 * * * * 160 = 50,079

13 Representations Decimal (default) Binary Hexadecimal 108 0’01101100
0x6C 108d b 6Ch 10810 6C16 108 dec bin 6C hex Group by 3s with a comma Group by 4s with a space or an underbar Generally don’t group Spoken short form

14 Decimal to binary Many ways, this is how I do it
120 120 < 128, 0 in 7th position 120 > 64, 1 in 6th position 120 – 64 = 56 56 > 32, 1 in 5th position 56 – 32 = 24 24 > 16, 1 in 4th position 24 – 16 = 8 8 = 8, 1 in 3rd position 8 – 8 = 0 Once we hit zero, all other values are zero Why do I do it this way? It is quick when I don’t have a calculator. Your mileage may vary Binary Decimal 1 10 2 100 4 1000 8 10000 16 100000 32 64 128

15 Decimal to binary Many ways, this is how I do it
120 120 < 128, 0 in 7th position 120 > 64, 1 in 6th position 120 – 64 = 56 56 > 32, 1 in 5th position 56 – 32 = 24 24 > 16, 1 in 4th position 24 – 16 = 8 8 = 8, 1 in 3rd position 8 – 8 = 0 Once we hit zero, all other values are zero Why do I do it this way? It is quick when I don’t have a calculator. Your mileage may vary Binary Decimal 1 10 2 100 4 1000 8 10000 16 100000 32 64 128

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

17 Representations Decimal (default) Binary 108 0’01101100 108d
10810 108 dec bin Group by 3s with a comma Group by 4s with a space or an underbar

18 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

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

20 Binary (Bitwise) And - &
1 X 194 & 225 = 227 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)

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

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

23 Binary (Bitwise) Not (complement) - ~
1 X 194 ^ 225 = 35 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)

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

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

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

27 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

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

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


Download ppt "Lecture 2: Logic Bryan Burlingame 31 Aug 2016."

Similar presentations


Ads by Google