Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Boolean Algebra Basics - AND / OR / NOT / XOR

Boolean Algebra Basics -- AND / OR / NOT / XOR

Boolean algebra is the mathematical foundation of all digital circuits; combinations of the four basic logic operations can build arbitrarily complex circuits.


Real-life analogy: automatic doors and access control systems

When entering a highly secure building, you will encounter various access rules. These rules correspond exactly to the basic operations of Boolean algebra; let's understand them with real-life scenarios.

AND gate -- both doors must open to enter: a vault requires two keys turned simultaneously to open the door. Only when both keys are inserted and turned (both inputs are 1) will the door open (output is 1). If either key is missing, the door does not budge.

OR gate -- any open door lets you enter: an office building has two entrances; as long as either the front door or the back door is open, you can go in. If any entrance is open (at least one input is 1), you can pass (output is 1).

NOT gate -- a button flips the indicator sign: at a restroom door there is a "Vacant/In Use" flip sign; pressing the button flips the current state. Input 0 (vacant) gives output 1 (in use), and vice versa.

XOR gate -- the room's two lamps must be one on and one off: a room has two lamps, and the rule is that the two lamps must be in opposite states (one on, one off), otherwise the alarm system is triggered. So the output is 1 when the two inputs differ, and 0 when they are the same.


A brief history of Boolean algebra

Boolean algebra was first systematically proposed by the British mathematician George Boole in 1854 in his work "An Investigation of the Laws of Thought". Boole's goal was to mathematize human logical reasoning; he created a method of expressing "true" and "false" with algebraic symbols.

In 1938, Claude Shannon proved in his MIT master's thesis that Boolean algebra can be used to describe and simplify relay switching circuits. This work, called "the most important master's thesis in history", laid the theoretical foundation for the entire field of digital circuit design.

It can be said that without Boolean algebra there would be no digital circuits, and without digital circuits there would be no modern computers. Inside the device on which you are reading this text, hundreds of millions of Boolean operations are being performed every second.


Detailed explanation of the four basic logic operations

AND operation

The rule of the AND operation is just one sentence: the output is 1 only when all inputs are 1; otherwise the output is 0. You can think of it as switches in series -- both switches must be closed for the bulb to light.

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

Logical expression: Y = A * B (sometimes the dot is omitted, written directly as Y = AB). Mnemonic: all 1 gives 1.

OR operation

The rule of the OR operation: as long as one input is 1, the output is 1; the output is 0 only when all inputs are 0. Analogy: switches in parallel -- if any switch is closed, the bulb lights.

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

Logical expression: Y = A + B (note this is not arithmetic addition; 1+1 in Boolean algebra still equals 1). Mnemonic: any 1 gives 1.

NOT operation

NOT is the only basic operation that accepts a single input, with the simplest rule: the output is the opposite of the input. Input 1 gives output 0, input 0 gives output 1.

A NOT A
0 1
1 0

Logical expression: Y = A' or Y = NOT A. In circuit diagrams a small circle on the input line is often used to indicate inversion. Mnemonic: 0 becomes 1, 1 becomes 0.

XOR operation

The rule of XOR: the output is 1 when the two inputs differ, and 0 when they are the same. XOR can be implemented by combining AND, OR, NOT: A XOR B = (A' * B) + (A * B').

A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0

Logical expression: Y = A XOR B. XOR has many applications in adder design, parity checking, data encryption, and other fields. Mnemonic: different gives 1.


Logic gate symbols

In digital circuit diagrams, each basic logic operation has a corresponding standard logic gate symbol:

AND gate: the symbol is D-shaped -- a straight edge plus a semicircular arc, with two input lines on the left and one output line on the right.

OR gate: the symbol is shield-shaped -- curves that converge inward, with the rear end converging to a point, representing the logical meaning of "or".

NOT gate (inverter): a triangle followed by a small circle -- the triangle represents buffering/driving, the circle represents signal inversion.

XOR gate: similar in shape to the OR gate, but with an extra arc in front of the input-side arc to distinguish "exclusive or".

Any complex digital circuit -- including the arithmetic unit and controller inside a CPU -- can ultimately be decomposed into combinations of these four basic logic gates. This is the fundamental reason Boolean algebra serves as the theoretical foundation of digital circuits.

Appendix: logic gate SVG icons and interactive truth tables (runoob demo)

The following shows the standard symbols of the four basic logic gates, along with an interactive truth table -- click the A and B buttons to toggle the inputs and observe the output changes in the corresponding rows.

Four standard logic gate symbols

A B Y

AND gate

All 1 gives 1 - D-shaped symbol

A B Y

OR gate

Any 1 gives 1 - shield-shaped symbol

A Y

NOT gate

Inversion - triangle + circle

A B Y

XOR gate

Different gives 1 - OR + arc

Interactive truth table -- click the buttons to toggle inputs

Click the A and B buttons below to toggle between 0/1 and observe the corresponding input row highlighted in the table

Input A

0

Input B

0

Currently selected:

A=0, B=0

A B AND OR NOT A NOT B XOR
0 0 0 0 1 1 0
0 1 0 1 1 0 1
1 0 0 1 0 1 1
1 1 1 1 0 0 0

Interactive demo

Click the buttons below to toggle the values of input A and B (0 or 1) and observe how the output of each logic gate changes in real time:

Logic gate interactive demo (runoob demo)

Click the buttons below to toggle the values of input A and B

Input A

Input B

Logic gate Logical expression Current output Mnemonic
AND gate A AND B - All 1 gives 1
OR gate A OR B - Any 1 gives 1
NOT gate NOT A - 0 becomes 1, 1 becomes 0
NOT gate NOT B - 0 becomes 1, 1 becomes 0
XOR gate A XOR B - Different gives 1

Python code demo

Below we use Python to implement functions for the four basic logic operations, automatically generate complete truth tables, and verify De Morgan's laws at the same time:

Example

runoob test: complete Python implementation of Boolean algebra

def

AND

(

a

,

b

)

:

   

"""AND operation: all 1 gives 1"""

   

return

1

if

a

==

1

and

b

==

1

else

0

def

OR

(

a

,

b

)

:

   

"""OR operation: any 1 gives 1"""

   

return

1

if

a

==

1

or

b

==

1

else

0

def

NOT

(

a

)

:

   

"""NOT operation: inversion"""

   

return

1

if

a

==

0

else

0

def

XOR

(

a

,

b

)

:

   

"""XOR operation: different gives 1"""

   

return

1

if

a

!=

b

else

0

def

print_truth_table

(

title

,

func

,

inputs

)

:

   

"""General truth table printing function"""

   

print

(

f

"\n{'='*40}"

)

   

print

(

f

"  {title}"

)

   

print

(

f

"{'='*40}"

)

   

if

func.code.

co_argcount

==

1

:

       

print

(

f

" {'A':>4} | {'Output':>4}"

)

       

print

(

f

"  {'-'*13}"

)

       

for

a

in

inputs:

           

print

(

f

"  {a:>4} | {func(a):>4}"

)

   

else

:

       

print

(

f

" {'A':>4} | {'B':>4} | {'Output':>4}"

)

       

print

(

f

"  {'-'*20}"

)

       

for

a

in

inputs:

           

for

b

in

inputs:

               

print

(

f

"  {a:>4} | {b:>4} | {func(a, b):>4}"

)

Main program: print all truth tables

inputs

=

[

0

,

1

]

print

(

"runoob Boolean algebra truth table full demo"

)

print_truth_table

(

"AND operation"

,

AND

,

inputs

)

print_truth_table

(

"OR operation"

,

OR

,

inputs

)

print_truth_table

(

"NOT operation"

,

NOT

,

inputs

)

print_truth_table

(

"XOR operation"

,

XOR

,

inputs

)

De Morgan's law verification

print

(

f

"\n{'='*40}"

)

print

(

" De Morgan's law verification"

)

print

(

f

"{'='*40}"

)

print

(

" Law 1: NOT(A AND B) = NOT A OR NOT B"

)

print

(

" Law 2: NOT(A OR B) = NOT A AND NOT B"

)

for

a

in

inputs:

   

for

b

in

inputs:

        left1

=

NOT

(

AND

(

a

,

b

)

)

        right1

=

OR

(

NOT

(

a

)

,

NOT

(

b

)

)

        left2

=

NOT

(

OR

(

a

,

b

)

)

        right2

=

AND

(

NOT

(

a

)

,

NOT

(

b

)

)

       

print

(

f

"  A={a}, B={b}:"

)

       

print

(

f

" Law 1: {left1} = {right1} -> {'OK' if left1 == right1 else 'FAIL'}"

)

       

print

(

f

" Law 2: {left2} = {right2} -> {'OK' if left2 == right2 else 'FAIL'}"

)