Bit Masks with Arduino

Bit masks are used to access specific bits in a byte of data.

Bit masks are used to access specific bits in a byte of data. This is often useful as a method of iteration, for example when sending a byte of data serially out a single pin. In this example the pin needs to change it's state from high to low for each bit in the byte to be transmitted. This is accomplished using what are known as bitwise operations and a bit mask.

Bitwise operations perform logical functions that take affect on the bit level. Standard bitwise operations include AND (&) OR (|) Left Shift (

<<
) and Right Shift (
>>
).

The AND (&) operator will result in a 1 at each bit position where both input values were 1. For example:

1x: 10001101
2
3 y: 01010111
4
5x & y: 00000101

The OR (|) operator (also known as Inclusive Or) will result in a 1 at each bit position where either input values were 1. For example:

1x: 10001101
2
3 y: 01010111
4
5x | y: 11011111

The Left Shift (

<<
) operator will shift a value to the left the specified number of times. For example:

1y = 1010
2
3 x = y << 1
4
5yields: x = 10100

All the bits in the byte get shifted one position to the left and the bit on the left end drops off.

The Right Shift (>>) operator works identically to left shift except that it shifts the value to the right the specified number of times For example:

1y = 1010
2
3 x = y >> 1
4
5yields: x = 0101

All the bits in the byte get shifted one position to the right and the bit on the right end drops off.

For a practical example, let's take the value 170, binary 10101010. To pulse this value out of pin 7 the code might look as follows:

1byte transmit = 7; //define our transmit pin
2byte data = 170; //value to transmit, binary 10101010
3byte mask = 1; //our bitmask
4byte bitDelay = 100;
5
6void setup()
7{
8
9 pinMode(transmit,OUTPUT);
10}
11
12void loop()
13{
14
15 for (mask = 00000001; mask>0; mask <<= 1) { //iterate through bit mask
16
17 if (data & mask){ // if bitwise AND resolves to true
18
19 digitalWrite(transmit,HIGH); // send 1
20
21 }
22
23 else{ //if bitwise and resolves to false
24
25 digitalWrite(transmit,LOW); // send 0
26
27 }
28
29 delayMicroseconds(bitDelay); //delay
30
31 }
32}

Here we use a FOR loop to iterate through a bit mask value, shifting the value one position left each time through the loop. In this example we use the

<<=
operator which is exactly like the
<<
operator except that it compacts the statement

100000001
2& 10101010
3
4 ________
5
6 00000000

And our output pin gets set to 0. Second time through the loop the mask = 00000010, so our operation looks like:

100000010
2& 10101010
3
4 ________
5
6 00000010

And our output pin gets set to 1. The loop will continue to iterate through each bit in the mask until the 1 gets shifted left off the end of the 8 bits and our mask =0. Then all 8 bits have been sent and our loop exits.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.