25
Boolean Logic

Boolean+logic

Embed Size (px)

Citation preview

Boolean Logic

George BooleIn1854, George Boole published “An investigation into the Laws of Thought, on which are founded the Mathematical Theories of Logic and Probabilities.”

Boole outlined a system of logic and a corresponding algebraic language dealing with true and false values.

Boolean LogicBoolean logic is a form of mathematics in which the only values used are true and false.Boolean logic is the basis of all modern computing.There are three basic operations in Boolean logic – AND, OR, and NOT.

The AND Operation

ANDb

T F

aT T F

F F F

The AND operation is a binary operation, meaning that it needs two operands.

c = a AND bBoth a and b must be true for the result to be true.

The OR OperationThe OR operation is also a binary operation with two operands.

c = a OR bIf either a OR b is true, then the result is true.

ORb

T F

aT T T

F T F

The NOT OperationThe NOT operation is a unary operation with only one operand.

c = NOT (a)It simply reverses the true or false value of the operand.

NOT

aT F

F T

Let’s use Boolean logic to examine class.

Please stand up if you are:◦ girl◦ AND black hair◦ AND left handed

Please stand up if you are:◦ girl◦ OR black hair◦ OR left handed

And NOTHow has the group changed depending on the

logical operator used.

Logical ConditionsLogical comparisons that are either true or false are most often used as the basis for the true and false values in Boolean logic.They are often used for simple conditions in branching and looping instructions.

If (hours > 40) pay overtime

If (age < 12) stay in the back seat

While (count 10) print count increment count

Nesting When more than one element is in parentheses, the

sequence is left to right. This is called "nesting.“

◦ (foxes OR rabbits) AND pest control ◦ foxes OR rabbits AND pest control ◦ (animal pests OR pest animals) NOT rabbits

Order of precedence of Boolean operators

The order of operations is: AND, NOT, OR, XOR Parentheses are used to override priority. Expressions in parentheses are processed first. Parentheses are used to organize the sequence

and groups of concepts.

Write out logic statements using Boolean operators for these.

• You have a buzzer in your car that sounds when your keys are in the ignition and the door is open.

• You have a fire alarm installed in your house. This alarm will sound if it senses heat or smoke.

• There is an election coming up. People are allowed to vote if they are a citizen and they are 18.

• To complete an assignment the students must do a presentation or write an essay.

Basis for digital computers.

The true-false nature of Boolean logic makes it compatible with binary logic used in digital computers.

Electronic circuits can produce Boolean logic operations.

Circuits are called gates.◦ NOT◦ AND◦ OR

AND gate The AND gate has the following symbol and

logic table. Two or more input bits produce one output bit. Both inputs must be true (1) for the output to

be true. Otherwise the output is false (0).

A B Q

0 0 0

0 1 0

1 0 0

1 1 1

OR gate The OR gate has the following symbol and logic table. Two or more input bits produce one output bit. Either inputs must be true (1) for the output to be true.

A B Q

0 0 0

0 1 1

1 0 1

1 1 1

NOT gate The simplest possible gate is called an "inverter," or a NOT gate. One bit as input produces its opposite as output. The symbol for a NOT gate in circuit diagrams is shown below. The logic table for the NOT gate shows input and output.

A Q

0 1

1 0

Combine gates. Gates can be combined. The output of one gate can become the input of another. Try to determine the logic table for this circuit.

What happens when you add a NOT to an AND gate?

Not

A B Q

0 0 0

0 1 0

1 0 0

1 1 1

A B Q

0 0 1

0 1 1

1 0 1

1 1 0

Can you make an AND gate from an NAND?

A B Q

0 0 0

0 1 0

1 0 0

1 1 1

“Exclusive” gatesExclusively OR gate are true if either input is

true but not both.

A B Q

0 0 0

0 1 1

1 0 1

1 1 0

A B Q

0 0 1

0 1 0

1 0 0

1 1 1

Truth Tables

xy = x AND y = x * y x + y = x OR y x bar = NOT xAND is true only if OR is true if either NOT inverts the bitboth inputs are true inputs are true We will denote x bar as ~X

NOR is NOT of OR NAND is NOT of AND XOR is true if both inputsdiffer

Logic Gates

Here we see the logic gatesthat represent the booleanoperations previously discussed

XOR looks like OR but with the addedcurved line

We typically represent NOR and NAND by the two on the left, but the two on the right are also correct

How do we use gates to add two binary numbers?

Binary numbers are either 1 or 0, either on or off.

Have two outputs. Need a gate to produce each output.

0 0 1 1

+ 0 + 1 + 0 + 1

00 01 01 10

A B Q CO

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

An Example: Half Adder• There are 4

possibilities when adding 2 bits together:

0 + 0 0 + 1 1 + 0 1 + 1• In the first case, we have a sum

of 0 and a carry of 0• In the second and third cases,

we have a sum of 1 and a carry of 0

• In the last case, we have a sum of 0 and a carry of 1

• These patterns are demonstrated in the truth table above to the right

• Notice that sum computes the same as XOR and carry computes the same as AND

• We build an Adder using just one XOR and one AND gate

The truth table for Sum and Carryand a circuit to compute these

Full AdderThe half adder really only does half the work

◦ adds 2 bits, but only 2 bits If we want to add 2 n-bit

numbers, we need to also include the carry in from the previous half adder◦ So, our circuit becomes more

complicated In adding 3 bits (one bit

from x, one bit from y, and the carry in from the previous addition), we have 8 possibilities

◦The sum will either be 0 or 1 and the carry out will either be 0 or 1

◦The truth table is given to the right

Building a Full Adder Circuit

The sum is 1 only if one of x, y and carry in are 1, or if all three are 1, the sum is 0 otherwise

The carry out is 1 if two or three of x, y and carry in were 1, 0 otherwise◦ The circuit to the right captures

this by using 2 XOR gates for Sum and 2 AND gates and an OR gate for Carry Out