If you're reading this page you are not happy with just getting the code to work, you want to understand what is going on. There are many different ideas of how to describe what binary bit manipulation means, this is ours. Hope this helps.

Binary is the representation of a number in base-2 (0,1). Typically numbers are represented in base-10 (0,1,2,...,9) and need to be converted from base-10 to base-2 for use with computers. The use of base-2 values with companion quality control fields is to use the number as a binary statement of true/false. Therefore, it is best to not think of the number of powers of 2. That will just lead to confusion.

The code examples use bit-shifting routines to allow the use of test numbers instead of forcing the user to create the binary representation. The creation of a binary number for small number of tests is reasonable, although some may consider the creation of a binary number representing more than 12 tests difficult. The intent of this menthod is to retain the notion of test numbers.

```
tests = [1, 2, 3, 5]
mask = Sum( LeftShift(1, tests-1) )
```

This is a very condensed amount of code so let's take it step by step. First, we list the tests we want to set, [1, 2, 3, 5]. We can think of tests and bits interchangeably. To help, here are the first 16 base-10 numbers and the equivalent number in base-2.

```
base-10 | base-2
-----------------
1 | 000 001
2 | 000 010
3 | 000 011
4 | 000 100
5 | 000 101
6 | 000 110
7 | 000 111
8 | 001 000
9 | 001 001
10 | 001 010
11 | 001 011
12 | 001 100
13 | 001 101
14 | 001 110
15 | 001 111
16 | 010 000
```

The code is using the left shift function to take a number and shift the bit set a number of positions to the left. To make things simple the number we are shifting is alway the number 1 (000 001). So to make 2 we shift 1 by one bit. To make the number 4 we shift 1 by two bits. Because we only care about numbers where one bit is set, we will only create numbers like 1, 2, 4, 8, 16, ... Although, to get the shifting correct we need to start at 0 and move bits to the left. Because the function will only shift a number greater than 0 we need to start with 1 (000 001). This means to create the binary representation from a number that already has the first bit tripped we need to subtract 1 from the bit postion we want to set, effectivly shifting 1 less. That is why the code uses *tests-1*.

We are also taking advantage of the language's native vector processing so multiple numbers are created when *tests* is a vector of more than one value.

```
tests = [1, 2, 3, 5]
values_used = tests - 1
[0, 1, 2, 4] = values_used
result = LeftShift(1, values_used)
```

The result of this code is a vector of the same length as *tests* with bits 1, 2, 3 and 5 tripped. The fourth bit is not set.

```
This is the result vector in base-10
[1, 2, 4, 16] = result
This is the result vector in base-2
[000001, 000010, 000100, 010000]
```

This is not a single number representing all the bits, so we need to combine the bits tripped into a single number. To do this we just add the numbers together.

```
mask = Sum([1,2,4,16])
This is mask in base-10
23 = mask
This is mask in base-2 indicating bits 1,2,3,5 are set
010111 = mask
```

This *mask* number contains the test we want to use and using binary manipulation the operation is very efficient.

For many languages, you can just create the mask in binary directly, without resorting to any bit-shifting logic. So in python, just set mask='0b010111' and you're done.