Arduino programming guide series

Bitshift and bitwise OR operators

Programming a microcontroller entails much more bit-level manipulation than what is common in general computer programming. Bitwise operators like "<<", ">>" and "|" are essential. Let's look at two of the most common bitwise operators, bitshift right and bitwise OR.

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 "0001000" 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 "0010000".

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 "0001000", then "val | 1" would be “0001001” (since 1 = 0000001).

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

val    0 0 0 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".

And so on.

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

New to the Arduino?

Arduino Step by Step Getting Started is our most popular course for beginners.

This course is packed with high-quality video, mini-projects, and everything you need to learn Arduino from the ground up. We'll help you get started and at every step with top-notch instruction and our super-helpful course discussion space.

Last Updated 9 months ago.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}