C++ 从入门到入土(English Version) Section 1:Gates, Circuits and Boolean Algebra

Section 1 : Gates, Circuits and Boolean Algebra


  In Computer Science (the study of what is possible with computers), it is important to understand the machine that we work with. Computers are made up of thousands of electronic circuits. In this section we will describe the basic components of a circuit and how to design circuits for specific purposes.

Gates

The basic components of a computer are gates. These are real components that you can buy and use to make electrical circuits. We are interested in the NOT gate, the AND gate and the OR gate. We use diagrams to indicate the gates:
在这里插入图片描述   NOT          AND          OR
You should remember these symbols. It helps to imagine that the symbol for the AND gate is shaped like the D in AND that the symbol for the OR gate is curved on the left to fit the O of OR.

The AND gate and the OR gate have two input lines each and one output line. The NOT gate has one input line and one output line. It is possible for the AND and OR gates to have more than two input lines each – more about this later.

We construct truth tables to help us remember how each gate works:
在这里插入图片描述

These truth tables are drawn with “On” and “Off”. It is more usual to see truth tables showing TRUE
(or T) for On and FALSE (or F) for Off. Sometimes we also write 1 for On and 0 for Off.

The truth tables provide us with the following knowledge about gates:

NOT gatewhen the input line is on (or 1 or TRUE) then the output line is off ; when the input line is off (or 0 or FALSE) then the output line is on
AND gateif both input lines are on then the output line is on (otherwise it is off)
OR gateif both input lines are off then the output line is off (otherwise it is on)

Logic circuits

We connect gates with lines (think of them as electric wires) to create circuits and we include switches and lights as needed. A switch can set a line to be on or off. A light glows if its input line is on.

Example: What does this circuit do:
在这里插入图片描述
When we flip the switch on, the light goes off and when we flip the switch off, the light comes on. This effect is caused by the NOT gate in the circuit.

We now move on to a more interesting circuit to solve a specific problem, which is:
  Ed will take 159.102 if Dan does or Carol does
  Dan will take 159.102 if Ann does and Bob does not

We will construct a logic circuit that will automatically tell Ed what to do. We use electricity to represent what each person does – if they take 159.102 we switch on, otherwise we switch off.

First we construct a circuit to see what Dan will do:
在这里插入图片描述
We have labelled the diagram for one particular situation. Work through every situation and label the diagram for each one of those situations.

Now we can build this circuit into a bigger circuit to see what Ed should do:

在这里插入图片描述
Check every possibility for this circuit.

For example, what happens if Ann, Bob and Carol all do not take 159.102?

This circuit performs a logic function. A function is a rule that takes in some input and produces some output. We are familiar with functions which take in numbers and produce another number. For

Boolean Algebra

Logic circuits take up a lot of space and time. We need a more concise notation. Algebra in mathematics uses variables to represent numbers. In Boolean Algebra we use variables to represent
statements. The value of these statements can be TRUE or FALSE.

Example: Let R = “it is raining today” (this can be TRUE or FALSE)

Symbols in Boolean Algebra

We use ~ to represent not. Thus ~R means “it is not raining today”.

Assume N = “it is not raining today”. Then ~R = N.

Note that ~~R = R. This is true for any variable, i.e. it is true for any statement.

We use + to represent OR.

Thus R + S means: for the whole statement to be true – R must be true, or S must be true, or they must both be true.

(Aside: if we want R to be true or S to be true but not both to be true – we would use XOR).

We use ● to represent AND.

Thus R ● S means: for the whole statement to be true – R must be true and S must be true. (Note that ● is often written simply as a full stop like this: R.S)

Priority of Operations

Many of us learnt the rule BEDMAS to remind us of the order of priority of arithmetic operations.

The rule for Boolean Algebra is NOTANDOR (and also brackets).

Examples:

(Mathematics) 4 + 2 * 3 = 10 but (4 + 2) * 3 = 18

(Boolean Algebra) H + J ● K = TRUE but (H + J) ● K = FALSE
(assume H = TRUE, J = FALSE, K = FALSE)

Evaluating Expressions

In Mathematics we could have the expression x + y. We cannot evaluate the expression until we assign values to x and y. For example, if x = 2 and y = 3 then x + y = 5.

Similarly in Boolean Algebra we can evaluate expressions once we assign values to variables.

Assume P = FALSE and Q = TRUE and evaluate: (P + ~P) ● ~~Q

(P + ~P) ● ~~Q = (P + ~P) ● Q = (FALSE + TRUE) ● TRUE = TRUE ● TRUE = TRUE

Note that for any statement P + ~P is always true.
You need to be able to switch between Boolean Algebra, logic circuits and truth tables.

For example:

在这里插入图片描述

Boolean Expression: X = ~(H ● M) + (M ● ~Q)

X is known as the output of the circuit. We usually put brackets around each term.

The corresponding truth table is:

在这里插入图片描述

Some things to notice about the truth table:

We set up a pattern to draw the truth table – Q changes F,T,F,T,etc, M changes F,F,T,T,F,F,etc, and H changes in fours. By using this pattern we ensure that we cover all possible situations.

We notice patterns in the output, e.g. if H is FALSE then the output is true. Also if M is FALSE then the output is true. This enables us to take some short cuts in the design of the circuit.

If designing the circuit from the truth table, make sure that you include all situations where the output is TRUE. We can ignore any situations where the output is FALSE.

Summary
We study logic circuits because they are the basis for designing a computer.
We study Boolean Algebra as a short-hand notation for writing logic circuits. The Boolean Algebra also assists us to find ways of reducing the circuit by removing components, e.g. ~~R = R.
We use truth tables to check that our circuit is functioning for every situation.

Reducing Boolean Expressions

Let us set up the logic circuit for (P + ~P) ● ~~Q (We make a “black box” with 2 inputs and 1 output).

We can now reduce this circuit by using Boolean Algebra as follows:

Reduce (P + ~P) ● ~~Q

Step 1: ~~Q = Q so we reduce to (P + ~P) ● Q

Step 2: P + ~P = true so we reduce to true ● Q

Step 3: true ● Q = Q so we reduce to Q
在这里插入图片描述
Note that the two “black boxes” do the same thing – you can prove it with a truth table!
在这里插入图片描述
By using Boolean Algebra we can reduce Boolean expressions. Reduced Boolean expressions enable us to build smaller circuits in computers. Smaller circuits save space (computers get smaller with bigger memory) and money (less components so cheaper to build).

Axioms

Axioms are the laws or rules that form the foundation of any algebra.

The Axioms of Boolean Algebra

P + Q = Q + PP ● Q = Q ● PCommutative
(P + Q) + R = P + (Q + R)(P ● Q) ● R = P ● (Q ● R)Associative
P ● Q + P ● R = P ● (Q + R)(P + Q) ● (P + R) = P + (Q ● R)Distributive
TRUE + P = TRUE FALSE + P = PTRUE ● P = P FALSE ● P = FALSEIdentities
P + ~P = TRUEP ● ~P = FALSEComplement
P + P = PP ● P = PIdempotent
~~P = PInvolution

Because Boolean algebra only has two values (TRUE and FALSE) we can use truth tables to show that an axiom is true for every possibility.

For example:

Use a truth table to show that P ● Q + P ● R = P ● (Q + R) is always TRUE:
在这里插入图片描述

The truth table shows that the LHS = RHS for every possibility.

How to design a circuit

Assume we have a problem that requires a circuit – understand the problem.
Create a truth table to cover every possibility that will exist for the new circuit.
Convert the truth table into a Boolean expression.
Reduce the expression using the axioms of Boolean algebra.
Design a circuit that corresponds to the reduced Boolean expression.
Check that the circuit matches the truth table for every situation.
Example: the 3-way voting circuit
A. The Problem: there are three people on a committee. They are called A, B and C. When they
vote on a proposal, the proposal will be approved if there are 2 or 3 votes in favour of the proposal and
the proposal will fail if there are 0 or 1 votes for it. Design a voting circuit in which there is a switch
for each person and a light that comes on if the proposal has enough votes.

B. Truth Table: create a truth table to cover every possible situation
在这里插入图片描述
在这里插入图片描述
C. Convert the truth table to Boolean expression:
Ignore any rows with an output of F. Each row becomes a term. Link the terms together with OR.
So we get: A ● B ● C + A ● B ● ~C + A ● ~B ● C + ~A ● B ● C

D. Reduce the Expression: we start by increasing the number of terms using the axiom P = P + P.
We also make use of the axiom that P + Q = Q + P, so we get:
A ● B ● C + A ● B ● ~C + A ● B ● C + A ● ~B ● C + A ● B ● C + ~A ● B ● C
Now we use the axiom P ● Q + P ● R = P ● (Q + R) to get:
A ● B ● (C + ~C) + A ● C ● (B + ~B) + B ● C ● (A + ~A)
Now we use the axiom P + ~P = true and the axiom P ● true = P to get:
A ● B + A ● C + B ● C
Finally we again use the axiom P ● Q + P ● R = P ● (Q + R) to get:
A ● (B + C) + B ● C
IMPORTANT: check the reduced expression against every situation in the truth table!!
E. Design the circuit:
在这里插入图片描述

IMPORTANT: check that the circuit does what it is meant to do!!

De Morgan’s Laws

The English mathematician Augustus De Morgan (in about 1850) established two further laws:
~(P ● Q) = ~P + ~Q and ~(P + Q) = ~P ● ~Q

We can show that they are correct for every situation by looking at the truth tables, for example:
在这里插入图片描述

De Morgan’s Laws are important as they allow us to convert AND into OR or vice versa.
Let us study the following circuit:
在这里插入图片描述

The output is (P ● ~Q) = ((P + Q)) = P + Q. Thus this circuit does the same thing as an OR gate but it does not include an OR gate. De Morgan’s Laws enable us to build an OR gate by using
AND and NOT.

One of the components inside this circuit was:
在这里插入图片描述
This combination of AND and NOT occurs frequently so we create a new gate by combining them.
在这里插入图片描述
Similarly, we can develop the NOR gate with symbol and truth table as follows:
在这里插入图片描述
All other gates can be constructed by using NAND gates and NOT gates.

Gates with multiple inputs:
在这里插入图片描述
Two little rules that can be helpful when you are looking at gates with multiple inputs:
  OR gate: you need just one input line to be true to make the output true.
      the output is false only if all input lines are false.
  AND gate: you need just one input line to be false to make the output false
      the output is true only if all input lines are true.

Working through a typical problem

Here is a truth table. Write down the corresponding Boolean expression, reduce the expression and design the circuit. Check the final circuit against the truth table.

Note: you need to be able to answer this type of question from any point in the cycle:

Truth table → Boolean expression → circuit → truth table → ….

Example: start with the following truth table:
在这里插入图片描述
To construct the Boolean expression, ignore all rows ending with F and so we get:

A ● B ● C + ~A ● ~B ● C + ~A ● ~B ● ~C

Using the axiom P ● Q + P ● R = P ● (Q + R) we get: A ● B ● C + ~A ● ~B ● (C + ~C)

Using P + ~P = true and true ● P = P we get: A ● B ● C + ~A ● ~B

And using De Morgan’s Law we get: A ● B ● C + ~(A + B)

And now we can design the circuit
在这里插入图片描述

Now go back to the truth table and check the circuit against each row of the table. For example: if A is OFF (FALSE) and B and C are both ON (TRUE) then the output should be OFF (FALSE).

The R-S flip-flop (also called a latch)

The circuit for the R-S flip-flop (where S means Set and R means Reset):
在这里插入图片描述
This circuit is rather difficult to follow but we will just accept it and not try to figure out exactly how it works. Looking at the black box (simplified) version of the R-S flip-flip we can see that it has two input lines (R and S) and two output lines (Q and ~Q). When S is switched briefly on (and then off again), Q will be set to on (or TRUE or 1). When R is switched briefly on (and then off again), Q will be set to off (or FALSE or 0). Q is called the state of the flip-flop. This is a bistable circuit, i.e. it has two states (on and off). The flip-flop can be used as a memory cell capable of storing zero (off) or one (on). We want to store larger numbers so we group a number of cells together to form a memory location.
在这里插入图片描述
Thus a memory location is a group of flipflops (circuits) that work together to store a binary number represented by zeros and ones.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I find using C++more enjoyable than ever.C++’s support for design and programming has improved dramatically over the years,and lots of new helpful techniques have been developed for its use.However,C++is not just fun.Ordinary practical programmers have achieved significant improvements in productivity,maintainability,flexibility,and quality in projects of just about any kind and scale.By now,C++has fulfilled most of the hopes I originally had for it,and also suc- ceeded at tasks I hadn’t even dreamt of. This book introduces standard C++?and the key programming and design techniques supported by C++.Standard C++is a far more powerful and polished language than the version of C++intro- duced by the first edition of this book.New language features such as namespaces,exceptions, templates,and run-time type identification allow many techniques to be applied more directly than was possible before,and the standard library allows the programmer to start from a much higher level than the bare language. About a third of the information in the second edition of this book came from the first.This third edition is the result of a rewrite of even larger magnitude.It offers something to even the most experienced C++programmer;at the same time,this book is easier for the novice to approach than its predecessors were.The explosion of C++use and the massive amount of experience accu- mulated as a result makes this possible. The definition of an extensive standard library makes a difference to the way C++concepts can be presented.As before,this book presents C++independently of any particular implementation, and as before,the tutorial chapters present language constructs and concepts in a‘‘bottom up’’ order so that a construct is used only after it has been defined.However,it is much easier to use a well-designed library than it is to understand the details of its implementation.Therefore,the stan- dard library can be used to provide realistic and interesting examples well before a reader can be assumed to understand its inner workings.The standard library itself is also a fertile source of pro- gramming examples and design techniques. This book presents every major C++language feature and the standard library.It is organized around language and library facilities.However,features are presented in the context of their use. That is,the focus is on the language as the tool for design and programming rather than on the lan- guage in itself.This book demonstrates key techniques that make C++effective and teaches the fundamental concepts necessary for mastery.Except where illustrating technicalities,examples are taken from the domain of systems software.A companion,The Annotated C++Language Stan- dard,presents the complete language definition together with annotations to make it more compre- hensible. The primary aim of this book is to help the reader understand how the facilities offered by C++ support key programming techniques.The aim is to take the reader far beyond the point where he or she gets code running primarily by copying examples and emulating programming styles from other languages.Only a good understanding of the ideas behind the language facilities leads to mastery.Supplemented by implementation documentation,the information provided is sufficient for completing significant real-world projects.The hope is that this book will help the reader gain new insights and become a better programmer and designer. Acknowledgments In addition to the people mentioned in the acknowledgement sections of the first and second edi- tions,I would like to thank Matt Austern,Hans Boehm,Don Caldwell,Lawrence Crowl,Alan Feuer,Andrew Forrest,David Gay,Tim Griffin,Peter Juhl,Brian Kernighan,Andrew Koenig, Mike Mowbray,Rob Murray,Lee Nackman,Joseph Newcomer,Alex Stepanov,David Vandevo- orde,Peter Weinberger,and Chris Van Wyk for commenting on draft chapters of this third edition. Without their help and suggestions,this book would have been harder to understand,contained more errors,been slightly less complete,and probably been a little bit shorter. I would also like to thank the volunteers on the C++standards committees who did an immense amount of constructive work to make C++what it is today.It is slightly unfair to single out indi- viduals,but it would be even more unfair not to mention anyone,so I’d like to especially mention Mike Ball,Dag Br. .u ck,Sean Corfield,Ted Goldstein,Kim Knuttila,Andrew Koenig,Josée Lajoie, Dmitry Lenkov,Nathan Myers,Martin O’Riordan,Tom Plum,Jonathan Shopiro,John Spicer, Jerry Schwarz,Alex Stepanov,and Mike Vilot,as people who each directly cooperated with me over some part of C++and its standard library. Murray Hill,New Jersey Bjarne Stroustrup

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值