Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
A Q
1 0
0 1
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
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.
A B Q
0 0 0
0 1 1
1 0 1
1 1 0
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.
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
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.
S = A XOR B
C = A AND B
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
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.
An ALU uses millions or billions of circuits like this cascaded together to perform
calculations.
Boolean in programming
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:
Yes No Yes
No Yes Yes
No No No