Use of Logical Functions

Control Systems

Logical functions can be used in a program for a control system.

Example : Suppose a control system consists of 8 switches. These may be either OFF (0) or ON (1).

 It is possible to turn switches on by using the OR instruction.

Eg If switches 1, 6 and 8 are already ON and we want to turn on switches 3 and 4...

Switch 1 2 3 4 5 6 7 8
Current position
(1,6 and 8 are ON)
1 0 0 0 0 1 0 1
Use the instruction
OR  0011000
Instruction is OR 0 0 1 1 0 0 0 0
and the resulting output is...
Switches 3 and 4 are switched on 1 0 1 1 0 1 0 1

The switches can be switched OFF by using the XOR instruction...

Following on from the end of the above example, what would be the effect of the following instruction:

XOR 10011000 ?

(Click on the box to the right to see if you were right)

 

Masking

Another use for logical functions is in masking out bits of a number. For example, a character may be input from the keyboard and stored as its ASCII code.

The digit 5 for example would be stored as the ASCII pattern 00110101

To convert it into a binary number we need to 'mask out' the first 4 bits of the number. To do this we use the AND function...as shown in this table...

The character '5' is input 0 0 1 1 0 1 0 1
Use the mask 00001111...
AND 00001111 0 0 0 0 1 1 1 1
resulting in... 0 0 0 0 0 1 0 1
..and this is the binary equivalent of the number 5.

 

 
Encryption

Take any number A, XOR it with another number B resulting in C.

Now take C, XOR it again with B, and the result will be A.

This fact is used in encryption, where A is the original data, B is the encryption key and C is the coded data.

Demonstration using the encryption key 10101010...

A (the original data) 10001101
XOR B(the encryption key) 10101010
= C (coded data) 00100111
Data can now be transmitted to another computer before being decrypted
XOR B(encryption key) 10101010
= A (original data) 10001101

 

  Encryption keeps the hackers out!