binary logic

5
THE MISSION

Upload: frederico-d

Post on 27-Jul-2016

218 views

Category:

Documents


0 download

DESCRIPTION

A future interactive resource to deliver Logic in Computing lessons.

TRANSCRIPT

Page 1: Binary logic

THE MISSION

Page 2: Binary logic

Computers are made up of complicated hardware that stores and processes data. If you break a computer down into its most basic components you have mil-lions of circuits that either allow electricity to flow, or not.

Imagine a whole row of light switches that you can switch on and off in different combinations to mean different things. Each switch is either on or off. It only has two states. That is why everything stored in a computer can be stored as a series of 1’s and 0’s. This is called binary.

All modern computers, data and instructions are based on the binary system. This follows from the ease with which we can decide between two states - ON or OFF, TRUE or FALSE, 1 or O - using simple transistors and capacitors.

The memory on a computer uses many small transis-tors and capacitors to store data and it is possible to

wire these transistors together so that they can make simple logical calculations such as, for example: are both inputs 1? or is only one of the inputs 1?These simple circuits are called LOGIC GATES.

We also have diagrams to represent these gates.

We express this relationship between input and output as a truth table. It is more usual to use algebraic variables such as A B, C ... for inputs and P, Q, R as outputs.

INPUT A INPUT B

0

10

0

0

1

OUTPUT P

0

0

0

11

1

INCOMING MESSAGE

ALERTINCOMING MESSAGE

Page 3: Binary logic

INPUT A

1

0

OUTPUT P

1

0

A NOT gate (also often called Inverter) is a logic gate. It takes one input signal. In logic, there are usually two states, 0 (false) and 1 (true). The gate therefore sends 1 (true) as output, if it receives 0 (false) as input. Alterna-tively it received 1 (true) as input, and sends 0 (false) as output.

An AND gate has two inputs and one output that performs logical conjunction. The output of an AND gate is 1 (true) only when all of the inputs are 1 (true). If one or more of an AND gate’s inputs are 0 (false), then the output of the AND gate is 0 (false).

INPUT A INPUT B

0

10

0

0

1

OUTPUT P

0

0

0

11

1

ALERTALERT

INCOMING MESSAGEINCOMING MESSAGE

Page 4: Binary logic

INCOMING MESSAGE

INCOMING MESSAGE

ALERT

We need to understand Boolean algebra in order to use many facilities within programming languages. Writing a program requires us to identify a logical process that the computer can follow and we need to be able to provide suitable Boolean expressions for the computer to use when making decisions about what to do next.

We might want our program to stop if one of two things happens, for example if we find a matching item in some data or we reach the end of a data file.

An OR gate has two inputs and one output that performs logical disjunction. The output of an OR gate is 1 (true) when one or more of its inputs are 1 (true). If all of an OR gate’s inputs are 0 ( false), then the output of the OR gate is 0 (false).

The computer might be given an instruction that says:REPEAT...UNTIL data match found OR end of file reachedThe computer’s CPU will evaluate this expression using the logic:

INPUT A INPUT B

0

10

0

0

1

OUTPUT P

0

1

1

11

1

Is there a data match?

End of file reached?

NO

YESNO

NO

NO

YES

STOP

NO

YES

YES

YESYES

YES

Page 5: Binary logic

INCOMING MESSAGE

# BINARY/DENARY CONVERSIONS# CONSTANTS AND VARIABLES# DATA TYPES# STRUCTURES# PROGRAM FLOW CONTROL# SELECTION# ITERATION# _

This, of course, using the OR gate logic.

We will often require the computer to evaluate a logical expression in order to make a decision.

IF x > 10 AND y > 12 THEN ...

This tells the computer to check the values of x and y in the pro-gram and to do ‘something’ if both x is bigger than 10 and y is bigger than 12.

WHILE x < 10 AND NOT(end of file) DO

This tells the computer to check if x is less than 10 and that it has not reached the end of a data file. If both things are true it carries on and does what it is asked, but if they are not both true it will stop and move on to the next instruction in the program.

INPUT A INPUT B

0

10

0

0

1

OUTPUT P

0

1

1

11

1