This is a follow up to a separate article (boolean arrays) in this series, where I discussed boolean arrays.

Some of the feedback I received in relation to that article asked for an explanation of what the operators “**<<**“ (bitshift left) and “**|**” (bitwise OR) do.

In this article, I will explain the functionality of these operators, drawing from the code in boolean arrays article.

First, I will remind you of the relevant part from the example code from the boolean arrays article.

The symbol "<<" is the binary** ****bitshift left** operator.

It takes a number like "000**1**000" and shifts (moves) every bit in it to the left.

The number of shifts can be controlled through the parameter given on the right side of the operator. In this example, the parameter is “**1**”, so the result of this operation will be "00**1**0000".

When you add the assignment "**=**“ operator to the bit-shift operator, so that the two operators together are "**<<=**", then you have the **binary left shift with assignment** operator.

This will take the shifted value and store it in the "**val**" variable, on the left side of the expression.

The exact same, but in reverse, will happen if you use the **bitshift right operator** “**>>**”.

Next, "**|**" is the bitwise **OR** operator.

When you add the "**=**" operator, you have this compound operator: "**|=**".

This is the **bitwise inclusive OR with assignment** operator.

In the example code, it works as shorthand for "**val |= 1**".

The full hand notation would be "**val = val | 1**". It is similar to the way that the expression "**val += 1**" is short for "**val = val + 1**".

Here's an example:

If **val** is "000**1**000", then "**val | 1**" would be “000**1**00**1**” (since 1 = 0000001).

Therefore, "**val |= 1**" would result to **val** containing the value "000**1**00**1**”. Perhaps this make more sense if we arrange the OR calculation like this:

val 0 0 0 **1 **0 0 0

1 0 0 0 0 0 0 **1**

---------------

val 0 0 0 **1** 0 0 **1**

In binary OR, 1|1 = 1, 1|0 = 1, 0|1 = 1, and 0|0=0.

When the function starts execution, **val** is initialized to 0. Inside the "for" loop, the first time that the bitshift operator is called has no effect on the value of val. Bitshifting "**0000000**" will still give you "**0000000**".

But lets say that **arr[0] = 1**.

When the sketch hits the next line, "**if (arr[i]) val |= 1;**", then val will become "**0000001**".

And when the block loops back to the "for" instruction in line 4, and the bitshift operation is executed again (**"val <<= 1;"**), then **val** will become "**0000010**".

See how essentially the contents of the arr array are copied into the val byte variable, one bit at a time?