Bitwise Boolean Logic

The KS3 National Curriculum for Computing requires that students know binary and Boolean logic, and also that they are able to carry out simple operations on binary numbers.  If you understand binary and you understand Boolean logic, then this could be one such operation - it's simply a combination of the two. Bitwise operations convert numbers to their binary equivalents and then apply logical operators to them a bit at a time.

128
0
64
0
32
0
16
0
8
0
4
0
2
0
1
0
=

AND
AND
AND
AND
AND
AND
AND
AND

128
0
64
0
32
0
16
0
8
0
4
0
2
0
1
0
=

=
=
=
=
=
=
=
=

128
0
64
0
32
0
16
0
8
0
4
0
2
0
1
0
= 0


You can change the numbers either by clicking on the 0s and 1s, or by typing numbers into the boxes. Try some examples - e.g. 3 OR 5 = 7. Can you see why? Look at the right-most bits - 1 OR 1 = 1. The next bits give 1 OR 0 = 1, and, in the 4 column, 0 OR 1 = 1. This means that the right-most three bits in the answer are 111 - i.e. 7.

This technique is used to mask bits to separate binary flags (amongst other things). Give it a try - enter a number and then AND it with 16; the answer will tell you whether the number includes History.

For a more in-depth discussion of this and other related techniques, look at the Bitwise Logic page in the Mathematics section.

Why not practice your programming skills by creating a program that uses these techniques? Bitwise logic can be used to convert decimal numbers to binary, or you can use Exclusive-OR to encrypt text.  This technique is also used in my traffic light and seven-segment display examples to indicate which lights should be lit for each stage in the sequence.  Click here to download some curriculum programming examples.