View
218
Download
1
Embed Size (px)
Citation preview
BOOLEAN LOGICBOOLEAN LOGIC
CSC 171 FALL 2004
LECTURE 7
ASSIGNMENTASSIGNMENT
Review Quiz # 2
Start reading Chapter 5
HistoryHistoryBoolean LogicBoolean Logic
1854 - George Boole describes his system for symbolic and logical reasoning:
An Investigation into the Laws of thought
Boolean logic later becomes the basis for computer design
QuoteQuote
“No matter how correct a mathematical theorem may appear to be, one ought never to be satisfied that there was not something imperfect about it until it also gives the impression of being beautiful.”
George Boole - Quoted in D MacHale, Comic Sections (Dublin 1993)
Basic IdeaBasic Idea
Boolean Logic is two valued logic– True– False
JAVAJAVA
The strings “true” and “false” are keywords in JAVA– Constants representing boolean value
Variables can be declared to as booleanboolean b1, b2 ;
Variables can be assigned a valueb1 = true ;b2 = false ;
Relational OperatorsRelational Operators< // less than<= // less than or equal to> // greater than>= // greater than or equal
to == // equal to!= // not equal to
ExamplesExamples( 4 < 5) ( 4 <= 3) ( 3 > 3)( 3 >= 3) (7 == 2)( 2 != 7)
Example valuesExample values( 4 < 5)
( 4 <= 3) ( 3 > 3)( 3 >= 3) (7 == 2)( 2 != 7)
truefalsefalsetruefalsetrue
Example assignmentsExample assignmentsboolean b1, b2, b3, b4, b5, b6;b1 = ( 4 < 5); b2 = ( 4 <= 3); b3 = ( 3 > 3);b4 = ( 3 >= 3); b5 = (7 == 2);b6 = ( 2 != 7);
Can be more complexCan be more complexboolean b1, b2;b1 = ( 4 < (3 + 2));int x = 7; b2 = ((x – 5) <= 3);
““AND” OPERATIONAND” OPERATION
A B A && B
False False False
False True False
True False False
True True True
Using ANDUsing ANDboolean b1 ;double inc = 73000.0;int numChild = 5;11 = ((inc < 50000.0) && (numChild >
3));
““OR” OPERATIONOR” OPERATION
A B A || B
False False False
False True True
True False True
True True True
Using orUsing orboolean b2 ;double inc = 73000.0;int numChild = 5;b2 = ((inc < 50000.0) || (numChild >
3));
““NOT” OPERATIONNOT” OPERATION
A !A
FALSE TRUE
TRUE FALSE
Using notUsing notboolean b3 ;double inc = 73000.0;int numChild = 5;b3 = !(!(inc < 50000.0) && !(numChild >
3));
Multiple modes of expressionMultiple modes of expression
We can express booleans in many ways– Table– Formula– Symbolic diagram
The mathematical basis for The mathematical basis for computer designcomputer design
An open switch can represent– Zero– False
A closed switch can represent– One– True
A switch can control another switch– A logic gate
Boolean ArithmeticBoolean Arithmetic
-True → False
-False → True
False + False → False
False + True → True
True + False → True
True + True → True
False * False → False
False * True → False
True * False → False
True * True → True
Boolean ArithmeticBoolean Arithmetic
-(0) → 1 //looks funny
-(1) → 0 // more like -1,1
0 + 0 → 0
0 + 1 → 1
1 + 0 → 1
1 + 1 → 1
(no “carry”)
0 * 0 → 0
0 * 1 → 0
1 * 0 → 0
1 * 1 → 1
(intuitive)
Boolean ArithmeticBoolean Arithmetic
-(0) → 1 // NOT
-(1) → 0
0 + 0 → 0
0 + 1 → 1
1 + 0 → 1
1 + 1 → 1
OR
0 * 0 → 0
0 * 1 → 0
1 * 0 → 0
1 * 1 → 1
AND
NotationNotation
To keep things straight
Boolean negation “!” replaces “-”Boolean addition “ || “ replaces “+”Boolean multiplication “&&” replaces “*”
Boolean Operations & CircuitsBoolean Operations & Circuits
AND
OR
NOT
X
Boolean Expressions & CircuitsBoolean Expressions & Circuits
AND: z = x && y ;
OR: z = x || y ;
NOT: z = !x ;
X
xy z
xy z
zx
Some practiceSome practice
Express the “circuit” as– A truth table– A formula (boolean assignment in JAVA)
So what?So what?
We can represent numbers in binaryWe can perform operations on booleanSo, we can use boolean circuits to automate
arithmetic operations
Binary AdditionBinary Addition
0 + 0 = 00
0 + 1 = 01
1 + 0 = 01
1 + 1 = 10
A + B = CS
AdditionAddition
0 + 0 = 00
0 + 1 = 01
1 + 0 = 01
1 + 1 = 10
A + B = CS
A B C S
0 0 0 0
0 1 0 1
1 0 0 1
1 1 0 0
QUESTION: “Carry in”QUESTION: “Carry in”
0 + 0 + 0 = ??
0 + 0 + 1 = ??
0 + 1 + 0 = ??
0 + 1 + 1 = ??
1 + 0 + 0 = ??
1 + 0 + 1 = ??
1 + 1 + 0 = ??
1 + 1 + 1 = ??
QUESTION: “Carry in”QUESTION: “Carry in”
0 + 0 + 0 = 00
0 + 0 + 1 = 01
0 + 1 + 0 = 01
0 + 1 + 1 = 10
1 + 0 + 0 = 01
1 + 0 + 1 = 10
1 + 1 + 0 = 10
1 + 1 + 1 = 11