• EUR€
  • £GBP
  • $USD

ESP32 / ESP8266 Arduino Tutorial: Bitwise AND operator

DFRobot Jul 31 2018 1550


In this guide, we will check how to use the bitwise AND operator on the Arduino core, running on the ESP32 and on the ESP8266.

Note that this is a C/C++ operator, which means that we can use it in programs for other microcontrollers supported on the Arduino environment and also in general C/C++ programs.

In binary, the AND operator is applied to two bits and the result is a bit. The resulting bit is 1 if both the input bits are 1 and 0 for every other combination of inputs. You can check below at figure 1 the truth table for the AND operator, with all input combinations.

AND logic operator truth table

Figure 1 – Truth table for the bitwise AND operator.

In C/C++, when we apply the bitwise AND operator to two numbers, it will apply the binary AND to each pair of bits in the same position.

So if we have the binary numbers 00001111 and 11110011, we will end with 00000011.

The syntax for this operator is shown below, where we are applying the AND operator to x and y.

x & y

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.

The code

The code for this tutorial will be really simple and we will write it on the Arduino setup. To get started, we open a Serial connection to output the results of our program.


Then we will declare two numbers and apply the bitwise AND operator to them.

First we are going to declare the value 10, which is represented in binary as 00001010. Note that this corresponds only to the least significant byte, since the remaining bits of the other bytes that compose our integer value are zero.

int x = 10;

Just as a note, in both platforms an integer is represented with 4 bytes. You can confirm this using the sizeof function, applied to the int data type.

Now we will declare the number 3, which corresponds to 00000011 in binary.

int y = 3;

Finally, we will print the result of applying the bitwise AND operator to both numbers. The expected result is 00000010, which corresponds to the decimal number 2.

int z = x & y;


The final source code can be seen below.

void setup() {

  int x = 10;
  int y = 3;
  int z = x & y;



void loop() {

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. Then, open the serial monitor. You should get an output similar to figure 2, which outputs the value 2, as we analyzed before.

ESP33 ESP8266 Arduino bitwise AND operator.png

Figure 2 – Result of the bitwise AND operation.

Just to facilitate the confirmation of the bitwise binary operation, below at figure 3 is the representation of the numbers we used, in binary.

bitwise AND operation between 10 and 3

Figure 3 – Bitwise AND operator applied to the numbers 10 and 3.