Sei sulla pagina 1di 10

Logic

Computer logic is an aspect of computer design concerning the fundamental


operations and structures upon which all computer systems are built.

Boolean

The Boolean data type has only two values: true or false. These values are used to
control the flow of the execution of programs. Boolean values are found by
comparing other data values. The results of these comparisons may be combined
in Boolean expressions.

Logic gates

Many electronic circuits have to make decisions. They look at two or more inputs and
use these to determine the outputs from the circuit. The process of doing this uses
electronic logic, which is based on digital switches called gates.

Logic gates allow an electronic system to make a decision based on a number of its
inputs. They are digital electronic devices. Each input and output of the gates must
be one of two states:

 true or 1 or 'on'

 false or 0 or 'off'
A single digital signal can be either on or off - for example, a light with one switch can
be on or off. However, if there is more than one signal, there are more than two
possible states. For example, if two signals are present there are four possible
combinations: on/on, on/off, off/on and off/off.

In a logic gate, each combination can be made to produce a different


outcome. Binary numbers reflect the two states - on and off, 1 and 0, true and false -
within CPU transistors. Logic gate calculations can also be represented as truth
tables.

Boolean algebra

Boolean algebra and truth tables can be used to describe logical expressions. The
most common Boolean operators are AND, OR and NOT (always in capitals). Each
operator has a standard symbol that can be used when drawing logic gate circuits.
NOT Gate

A NOT gate has just one input. The output of the circuit will be the opposite of the
input. If 0 is the input, then output is 1. If 1 is the input, then 0 is the output.

NOT Truth Table

A Q

1 0

0 1

Boolean expression Q = NOT A.

AND Gate

An AND gate can be used on a gate with two inputs. AND tells us that both inputs h
must be 1 in on order for the output to be 1.
AND Truth Table

A B Q

0 0 0

0 1 0

1 0 0

1 1 1

Boolean expression Q = A AND B.

OR Gate

The OR gate has two inputs. One or both inputs must be 1 to output a 1 otherwise
output is 0.

OR Truth Table

A B Q

0 0 0
A B Q

0 1 1

1 0 1

1 1 1

Boolean expression Q = A OR B.

XOR gate

The XOR gate works the same as an OR but will only output 1 if one or the other
inputs are 1.

XOR Truth Table

A B Q

0 0 0

0 1 1

1 0 1

1 1 0

Boolean expression Q = A XOR B.

Complex logic gates

Logic gates can be built up into chains of logical decisions. Some logic gates may
have more than two inputs. The diagram below shows a complex logic
gate combining three simple gates.
Altogether there are three inputs and eight possible outcomes. To solve the truth
table below, first find D, then E and finally Z. Complete a whole column before
moving on to the next column. D depends only on A, E depends on B and C, and Z
depends on E or D.

Complex Truth Table

A B C D = NOT A E = B AND C Z = D OR E

0 0 0 1 0 1

0 0 1 1 0 1

0 1 0 1 0 1

0 1 1 1 1 1

1 0 0 0 0 0

1 0 1 0 0 0

1 1 0 0 0 0

1 1 1 0 1 1

Z = D OR E or Z = NOT A OR (B AND C).


Logic gates in the CPU

The following example demonstrates how the ALU uses logic gates to
perform binary addition. It combines two gates, in parallel. There are two inputs and
each gate has a single output - so in total there are two outputs, with four possible
outcomes.

The Boolean expressions for this circuit are:

S = A XOR B

C = A AND B

Logic CPU Truth Table

A B S = A XOR B C = A AND B

0 0 0 0

0 1 1 0
A B S = A XOR B C = A AND B

1 0 1 0

1 1 0 1

Half adder - binary addition

This gate models a 1-bit binary sum. The possible calculations of a 1-bit binary sum
are:

1. 0+0=0

2. 0+1=1

3. 1+0=1

4. 1+1=10

S and C together represent the output of the sum of 'A + B'. Each gate can only
output a single bit - a number with one place value. 1+1 creates 10 which needs 2
place values, so this is an overflow number. In overflow sums, the result is recorded
as 0 and the 1 has to be carried. S represents the recorded 0 and C represents the
carried 1.

This configuration is called a binary half adder. It is a good way to begin to


understand how an ALU is able to perform arithmetic. To perform complete arithmetic
that can take account of carry bits and add more than two bits together, two half
adders must be combined together to make a full adder.

An ALU uses millions or billions of circuits like this cascaded together to perform
calculations.

Boolean in programming

Boolean algebra is used frequently in computer programming. A Boolean


expression is any expression that has a Boolean value. For example, the
comparisons 3 < 5, x < 5, x < y and Age < 16 are Boolean expressions.

 The comparison 3 < 5 will always give the result true, because 3 is always
less than 5.

 The comparison x < 5 will give the result true when the variable x contains a
number less than 5, and false when it contains any number that is greater than or
equal to 5. The variable x must contain a number for this comparison to make
sense.

 The comparison x < y will give the result true when the variable x contains a
value that is 'less than' the value contained by the variable y. Note that in
some programming languages the 'less than' operation is only defined for
numbers, but in others it is defined for other data types as well.
Computer programmers use Boolean values to control selection and repetition in
programs. For example, in a cinema computer system Boolean could be used to
program the type of tickets people should get. Here it is written in pseudocode:

Ask the user to type in their age in whole years Store the input data in the variable
Age If Age<16 Set EntranceFee = 500 Otherwise Set EntranceFee = 1000

NOT, AND, OR

Boolean expressions may combine two or more Boolean values using the
operations NOT, AND and OR. This is used in many different algorithms.

For example, a cinema computer system could assess if people under 16 and over
65 are charged the concession rate. This is the algorithm in pseudocode:

Ask the user to type in their age in years Store the input data in the variable Age If
Age<16 OR Age>65 Set EntranceFee = 500 Otherwise Set EntranceFee = 1000

In this example, the expression Age < 16 OR Age > 65 will give the value 'true' when
the user's age is less than 16 or when it is greater than or equal to 65, otherwise it
will give the value 'false'.

It is also possible to create variables that will hold Boolean values for later use.
The variables in this scenario are 'Age', 'EntranceFee' and 'Concession'. 'Age' can be
any integer, 'Concession' is used to hold a Boolean value and 'EntranceFee' is set
according to the status of 'Concession' (500 or 1000). This can be expressed in
pseudocode as follows:

IF Age<16 OR Age>65 THEN Concession IF Concession THEN EntranceFee = 500


IF NOT Concession THEN EntranceFee = 1000

The truth table for this system will be:


Age under 16? Age 65+? Concession?

Yes No Yes

No Yes Yes

No No No

Potrebbero piacerti anche