csc 107 – programming for science. follow-up from last lecture
TRANSCRIPT
LECTURE 8:BOOLEAN LOGIC
CSC 107 – Programming For Science
Follow-Up From Last Lecture
Announcements
Lectures may not cover all material from book Lectures will cover most difficult or
challenging topics All material is important & you are
responsible for it For class to work, you must read & ask
questions
George Boole
Mathematician from English middle-class Lived from 1815 – 1864 Started work at age 16 as a teaching
assistant Held two assistantships to support family Opened own school after many years of
work In 1847 wrote Mathematical Analysis of
Logic
Mathematical Analysis of Logic Boole’s book proposed new logical
system World began with 2 values– though more
created Devised rules to add, subtract, & multiply
Work ignored during Boole’s lifetime System only had 2 values, so what was the
point? What is done with developer of pointless
knowledge? Basis for most technology in the
modern age All it took was a simple little discovery…
Gate
Combines input(s) to generate output signal Like most electronics, uses “on-off” state Input is "off", if line drops below 3.3 volts From 3.3 - 5 volts, an input is considered on Gate is deep fried silicon if line goes above
5 volts, Like Boole’s logic, electronics have 2
values Simple gates combine to make modern
circuitry All initially part of Boolean algebra Basis of programming at the lowest, rawest
level
Truth Table
Normal way that Boolean functions presented
All combinations of inputs shown in this table This is really easy, inputs must be true or
false Output shown for each of the possible
inputs Given how it sounds, not at all
complicated Very simple rules to follow to construct Does requires you count up to 2
NOT Gate
Simplest gate: computes opposite of input Output false when input true; Output true when input false;
Written in C++ as !a a is gate’s inputa !a
true
false
a !a
OR Gate
Equivalent to addition in Boolean algebra If either input is true is going to be checked true when either a OR b are true; false
otherwise Written in C++ as a || b
a & b are both inputs to gatea b a || b
false falsefalse truetrue falsetrue true
a
ba || b
AND Gate
Equivalent to multiplication in Boolean algebra If both inputs are true is going to be
checked True when a AND b are true; false otherwise
Written in C++ as a && b a & b are both inputs to gate
a
ba && b
a b a && b
false falsefalse truetrue falsetrue true
Boolean Values
3 boolean functions can combine into computer Billions of gates combined by engineers to
make CPU Could use, for example: (a && b) || (a && !c)
But how to generate inputs for these functions First need boolean values of true and false Easy in hardware (true is any signal > 3.3V) But how could we do get values within our
program?
Relational Operators
< (less than) > (greater than) <= (less than of equal to) >= (greater than of equal to) != (inequality ≠) == (equality – if two things have same
value)
Relational Operators
< (less than) > (greater than) <= (less than of equal to) >= (greater than of equal to) != (inequality ≠) == (equality – if two things have same
value) Equality (==) and assignment (=) are NOT
the same
Assignment vs. Comparison
Assign values using "=" (single equals sign) Sets left-side variable equal to value on
right Compare values using "==" (double
equal sign) However it is used, computes boolean value
Very easy to get them confused Frequently (& easy) bug found everywhere Assignment treated as boolean expression,
also Code will compile, but will not like
unexpected results
Relational Operators
Relational operators compute bool Like any expression, can be used in any
statements
int nfl = 32;bool team = 0 > 6;bool group = 45 <= nfl;bool gang = nfl == 32;team = sqrt(144) == 12.0;group = (133 == pow(12, 2));gang = (group == team);
Your Turn
Get in groups & work on following activity
For Next Lecture
Read sections 2.1 – 2.9 for Monday Why do programmers never wash their
hair? How do we discuss & display program
ideas? Why do coders have flow? How can we
chart this?
Week #3 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes,
TALK TO ME!