Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)

The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. Much of the material below is from an excellent tutorial on bitwise math wihch may be found here.

Description and Syntax

Below are descriptions and syntax for all of the operators. Further details may be found in the referenced tutorial.

Bitwise AND (&)

The bitwise AND operator in C++ is a single ampersand, &, used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0. Another way of expressing this is:

        0 0 1 1   operand1
        0 1 0 1   operand2
        0 0 0 1   (operand1 & operand2)    –    returned result

In 86Duino, the type int is a 32-bit value, so using & between two int expressions causes 32 simultaneous AND operations to occur. In a code fragment like:

    int a =  92;    // in binary: 00000000000000000000000001011100
    int b = 101;    // in binary: 00000000000000000000000001100101
    int c = a & b;  // result:    00000000000000000000000001000100, or 68 in decimal.

Each of the 32 bits in a and b are processed by using the bitwise AND, and all 32 resulting bits are stored in c, resulting in the value 01000100 in binary, which is 68 in decimal.

One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking.

Bitwise OR (|)

The bitwise OR operator in C++ is the vertical bar symbol, |. Like the & operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0. In other words:

        0 0 1 1    operand1
        0 1 0 1    operand2
        0 1 1 1 (operand1 | operand2)    –    returned result

Here is an example of the bitwise OR used in a snippet of C++ code:

    int a =  92;    // in binary: 00000000000000000000000001011100
    int b = 101;    // in binary: 00000000000000000000000001100101
    int c = a | b;  // result:    00000000000000000000000001111101, or 125 in decimal.
Bitwise XOR (^)

There is a somewhat unusual operator in C++ called bitwise EXCLUSIVE OR, also known as bitwise XOR. (In English this is usually pronounced “eks-or”.) The bitwise XOR operator is written using the caret symbol ^. This operator is very similar to the bitwise OR operator |, only it evaluates to 0 for a given bit position when both of the input bits for that position are 1:

         0 0 1 1    operand1
         0 1 0 1    operand2
         0 1 1 0    (operand1 ^ operand2)    –    returned result

Another way to look at bitwise XOR is that each bit in the result is a 1 if the input bits are different, or 0 if they are the same.

Here is a simple code example:

    int x = 12;     // binary: 1100
    int y = 10;     // binary: 1010
    int z = x ^ y;  // binary: 0110, or decimal 6

The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise OR operation if there is a 1 in the mask bit, that bit is inverted; if there is a 0, the bit is not inverted and stays the same.

See also

&& (Boolean AND)
|| (Boolean OR)

Language Reference Home

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.