>: shift to the right <<: shift to the left"> >: shift to the right <<: shift to the left">
Download presentation
Presentation is loading. Please wait.
1
Logic operations
2
Logic operations We have already seen keywords or, and, not used in Python Had a specific purpose – Boolean expressions. For example: if x >= 0 and x < 10: print("x is a single digit") Python has a set of operators for bitwise computations: & : bitwise AND | : bitwise OR ~ : bitwise NOT >>: shift to the right <<: shift to the left
3
A bit of intuition… 21 << 1 5 | 6 5 & 6 21 >> 1 42 ? ? 10
& bit-wise AND | bit-wise OR ^ bit-wise XOR 21 >> 1 10
4
A bit of intuition… 21 << 1 5 | 6 5 & 6 ~0 21 >> 1
Sometimes the bits are almost visible: 10101 101 110 101 110 21 << 1 5 & 6 5 | 6 000 10101 & bit-wise and | bit-wise or ^ bit-wise exclusive or ~ bit-wise not NOTE ~0 is -1 because it flips all the bits including sign bit ~000 is 111 21 >> 1 ~0
5
A bit of intuition… 21 << 1 5 | 6 5 & 6 ~0 21 >> 1
Sometimes the bits are almost visible: 10101 101 110 101 110 21 << 1 5 & 6 5 | 6 101010 100 111 000 10101 & bit-wise and | bit-wise or ^ bit-wise exclusive or ~ bit-wise not NOTE ~0 is -1 because it flips all the bits including sign bit ~000 is 111 21 >> 1 ~0 111 1010
6
AND OR NOT Truth tables x y AND(x,y) x y OR(x,y) x NOT(x) 1 1 1 1 1 1
input output input output input output x y AND(x,y) x y OR(x,y) x NOT(x) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 AND outputs 1 only if ALL inputs are 1 OR outputs 1 if ANY input is 1 NOT reverses its input Let’s construct truth tables for the operations that we have just described. (They should have it in the handout, so we can just look over the handout) AND OR NOT
7
All computation x y fn(x,y) 1 1 1 1 1 1 Truth table
… consists of functions of bits, or boolean values Boolean inputs x,y,… can only be 0 or 1 (False or True). Boolean functions can output only 0 or 1 (False, True). inputs output x y fn(x,y) 1 1 1 1 1 1 Truth table
8
Lots of bits! x y z fn(x,y,z) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
output inputs x y z fn(x,y,z) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Truth table
9
Logic gates and circuits
10
Reviewing… We have: explored a wide range of data types learned how different encodings are used for different types learned that, at the core of all data stored in the computer are bits observed different operations that can be performed on these bits (AND, OR, NOT) We have one BIG QUESTION remaining… HOW IS COMPUTATION ACTUALLY CARRIED OUT?
11
In a computer, each bit is represented as a voltage (1 is +5v and 0 is 0v)
Computation is simply the deliberate combination of those voltages! Feynman: Computation is just a physics experiment that always works! 101010 42 110011 ADDER circuit 51 Fenyman – awesome physicist, amazing work in quantum mechanics, Nobel prize in 1965 9 001001
12
In a computer, each bit is represented as a voltage (1 is +5v and 0 is 0v)
Computation is simply the deliberate combination of those voltages! Feynman: Computation is just a physics experiment that always works! 101010 42 (1) set input voltages 000000 ADDER circuit 9 001001
13
In a computer, each bit is represented as a voltage (1 is +5v and 0 is 0v)
Computation is simply the deliberate combination of those voltages! 101010 42 (1) set input voltages 110011 ADDER circuit 51 (2) read output voltages 9 001001
14
In a computer, each bit is represented as a voltage (1 is +5v and 0 is 0v)
Computation is simply the deliberate combination of those voltages! HOW? The focus for this week: Learn to design circuits that can accomplish simple computations! 101010 42 110011 ADDER circuit 51 (2) read output voltages 9 001001
15
of these boolean functions…
We need only three building circuits to compute anything at all input output input output x y AND(x,y) x y OR(x,y) input output x NOT(x) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 input input x x y x y These are standard symbols for the gates. Reviewing our basic knowledge of these boolean functions… x + y x xy output "x or y" output "not x" "x and y"
16
Circuits from logic gates... ?
What are all of the inputs that make this circuit output 1?
17
Logisim HW 5 – Use Logisim (a free circuit simulation package) to design circuits to perform simple computations Hard? Well, let's recall our claim – we only need AND, OR and NOT to compute anything at all…
18
Constructive Proof ! xy + xy Formula !
For each input row whose output needs to be 1, build an AND circuit that outputs 1 only for that specific input! Specify a truth table defining any function you want i ii input output x y fn(x,y) iii 1 1 OR them all together 1 1 1 1 Formula ! Minterm Expansion Principle – algorithm for building expressions from truth tables xy + xy minterm
19
Minterm expansion readily converts into logic gates…
20
Constructive Proof ! xy + xy x y fn(x,y) 1 1 1 1 1 1
For each input row whose output needs to be 1, build an AND circuit that outputs 1 only for that specific input! Specify a truth table defining any function you want i ii input output x y fn(x,y) iii OR them all together 1 1 1 1 xy + xy 1 1 NOT AND Does this work for the red row? x y Does this work for any other rows?
21
Constructive Proof ! xy + xy x y fn(x,y) 1 1 1 1 1 1
For each input row whose output needs to be 1, build an AND circuit that outputs 1 only for that specific input! Specify a truth table defining any function you want i ii input output x y fn(x,y) iii OR them all together 1 1 xy + xy 1 1 1 1 How did we handle the rows outputting zero? NOT AND OR NOT AND x y
22
Constructive Proof ! For each input row whose output needs to be 1, build an AND circuit that outputs 1 only for that specific input! Specify a truth table defining any function you want i ii input output x y fn(x,y) This is a constructive proof that AND, OR, and NOT suffice to build any (boolean) function! iii 1 1 OR them all together 1 1 1 1 How did we handle the rows outputting zero? NOT AND OR NOT AND x y
23
But... ALL functions are just boolean functions: binary!
Constructive Proof ! For each input row whose output needs to be 1, build an AND circuit that outputs 1 only for that specific input! Specify a truth table defining any function you want i ii But... ALL functions are just boolean functions: binary! input output x y fn(x,y) This is a constructive proof that AND, OR, and NOT suffice to build any (boolean) function! iii 1 1 OR them all together 1 1 1 1 How did we handle the rows outputting zero? NOT AND OR NOT AND x y
24
Example
25
Try it! the less than or equal circuit (<=) Truth Table Formula
We usually know what we want to do… We just have to determine how to build it! Truth Table Formula "English" input bits output bit f(x,y) should output 1 when x <= y otherwise, output 0 x y x <= y 1 1 1 1 1,1,0,1 -x-y + -xy + xy = -x + xy BETTER -> recognize that only one case is a 0, and use a NOT Another circuit to do Less than or Equal 1 1 1 Circuit
26
Try it! xy + xy + xy the less than or equal circuit (<=)
We usually know what we want to do… We just have to determine how to build it! Truth Table Formula "English" input bits output bit f(x,y) should output 1 when x <= y otherwise, output 0 xy + xy + xy x y x <= y 1 1 1 1 1,1,0,1 -x-y + -xy + xy = -x + xy BETTER -> recognize that only one case is a 0, and use a NOT Another circuit to do Less than or Equal 1 1 1 Circuit
27
x <= y
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.