Masking

Setting a bit

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
Exercise

In this same example, 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 it is 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.