﻿ Binary Flags | Mathematics | Computing ﻿

# Binary Flags

The new KS3 National Curriculum for Computing requires you to study binary and its uses.  Here we look at how you can use binary and bitwise logic to use a single value to represent variable numbers of things - this can be useful in programming to pass values between functions.

In computing, a flag is a type of signal usually used to indicate whether something is true or false. To save time and make your program less complicated, you might want to combine these flags and send several pieces of information in one go.

Imagine you wanted to send a message to your friend to indicate which subjects had set homework on a particular day. If it was only one subject, you can just send the name of the subect - e.g. English - but if there is more than one, it gets more complicated. One way to do it is to give each subject a number:

 1 English 2 Maths 4 Science 8 Computing 16 History 32 Geography 64 French 128 German

You can send combinations of subjects by adding together the numbers and sending the total. Try it below:

Enter the homework code and click the button.

This only works because of the numbers I've chosen - it's a binary sequence, with each number being one of the binary column headings. This means that each total can only be made up from one combination of subjects.

If they'd been numbered as 1 = English, 2 = Maths, 3 = Science, 4 = Computing, etc., then it wouldn't work, because a code of 3 could represent English and Maths, or it could be Science on its own. To use this technique in your programming, you need to be familiar with bitwise logic.  I have used bitwise logic in my traffic light and seven-segment display examples to indicate which lights should be lit for each stage in the sequence.

For a more in-depth discussion of this and other similar techniques, look at the Bitwise Logic page in the Mathematics section. You can also watch a video on uses of binary in the Number Bases playlist on the Advanced ICT YouTube channel.