Lecture 2: Logic Bryan Burlingame 31 Aug 2016
Ref: xkcd: www.xkcd.com
Announcements Homework posted Homework 1 due next week E-mail: fall2016@me30.org Lab starts this week
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.
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
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
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 * 1023 + 0 * 1022 + 2 * 1021 010101012 = 8510= 1 * 27 + 1 * 26 + 1 * 24 + 1 * 22 + 1 * 20 Range? Largest 8 bit value: 111111112 = 25510 Largest 16 bit value: 11111111111111112 = 6553510
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 11010101 = -213, 01010101 = 213 1’s Complement Flip the bits (change 1’s into 0’s) 10101010 = - 213, 01010101 = 213
Negative Values (Cont) 2’s Complement Take 1’s complement, add one 01010100 Start (8410 in this example) 10101011 Flip bits 10101100 Add one (notice the carries)
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
Binary to Hexadecimal Convert Binary to Hex 1100001110011111b
Binary to Hexadecimal Convert Binary to Hex 1100 0011 1001 1111 b (grouping 4 bits) 12 3 9 15 d C 3 9 F h C39Fh 12 * 163 + 3 * 162 + 9 * 161 + 15 * 160 = 50,079
Representations Decimal (default) Binary Hexadecimal 108 0’01101100 0x6C 108d 0110 1100b 6Ch 10810 0110 11002 6C16 108 dec 0110 1100 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
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 0111 1000 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 1000000 64 10000000 128
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 0111 1000 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 1000000 64 10000000 128
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)
Representations Decimal (default) Binary 108 0’01101100 108d 10810 0110 11002 108 dec 0110 1100 bin Group by 3s with a comma Group by 4s with a space or an underbar
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 = 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 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 ^ 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) 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
Boolean Logic And && False True Exclusive Or (Xor) ^^ False True Or ||
Boolean Logic - Examples ( 6 < 14 ) && ( 9 > 5 ) 5 || (19 > 45) && (57 < 108) (My name is Bryan) && (This is ME30) (name == “Bryan”) && (class == “ME30”) ((16 + 45) == 61) ^^ ((49) == (7 * 7)) 42
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
References Nicholas, P. (1994). Digital Design. Minneapolis/St. Paul: West Publishing Company