Reference.BitwiseCompound History

Hide minor edits - Show changes to markup

October 21, 2010, at 02:17 PM by David A. Mellis -
Changed lines 3-137 from:

The compound bitwise operators perform their calculations at the bit level of variables. They are often used to clear and set specific bits of a variable.

See the bitwise AND (&) and bitwise OR (|) operators for the details of their operation, and also the Bitmath Tutorial for more information on bitwise operators.

compound bitwise AND (&=)

Description

The compound bitwise AND operator (&=) is often used with a variable and a constant to force particular bits in a variable to the LOW state (to 0). This is often referred to in programming guides as "clearing" or "resetting" bits.

Syntax:

x &= y;   // equivalent to x = x & y; 

Parameters

x: a char, int or long variable
y: an integer constant or char, int, or long

Example:

First, a review of the Bitwise AND (&) operator

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

Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if myByte is a byte variable,
myByte & B00000000 = 0;

Bits that are "bitwise ANDed" with 1 are unchanged so,
myByte & B11111111 = myByte;

Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)

Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100

   1  0  1  0  1  0  1  0    variable  
   1  1  1  1  1  1  0  0    mask
   ----------------------
   1  0  1  0  1  0  0  0

 variable unchanged
                     bits cleared
  

Here is the same representation with the variable's bits replaced with the symbol x

   x  x  x  x  x  x  x  x    variable
   1  1  1  1  1  1  0  0    mask
   ----------------------
   x  x  x  x  x  x  0  0

 variable unchanged
                     bits cleared
 

So if:

myByte =  10101010;

myByte &= B1111100 == B10101000;

compound bitwise OR (|=)

Description

The compound bitwise OR operator (|=) is often used with a variable and a constant to "set" (set to 1) particular bits in a variable.

Syntax:

x |= y;   // equivalent to x = x | y; 

Parameters

x: a char, int or long variable
y: an integer constant or char, int, or long

Example:

First, a review of the Bitwise OR (|) operator

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

Bits that are "bitwise ORed" with 0 are unchanged, so if myByte is a byte variable,
myByte | B00000000 = myByte;

Bits that are "bitwise ORed" with 1 are set to 1 so:
myByte | B11111111 = B11111111;

Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (|=) with the constant B00000011

   1  0  1  0  1  0  1  0    variable
   0  0  0  0  0  0  1  1    mask
   ----------------------
   1  0  1  0  1  0  1  1

 variable unchanged
                     bits set


Here is the same representation with the variables bits replaced with the symbol x

   x  x  x  x  x  x  x  x    variable
   0  0  0  0  0  0  1  1    mask
   ----------------------
   x  x  x  x  x  x  1  1

 variable unchanged
                     bits set

So if:

myByte =  B10101010;

myByte |= B00000011 == B10101011;

See Also

  • & (bitwise AND)
  • | (bitwise OR)
  • && (Boolean AND)
  • || (Boolean OR)
to:

See the individual pages for: &= (compound bitwise and) and |= (compound bitwise or).

October 21, 2010, at 02:12 PM by David A. Mellis -
Changed lines 102-104 from:

myByte & B11111111 = B11111111;

Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B00000011

to:

myByte | B11111111 = B11111111;

Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (|=) with the constant B00000011

September 08, 2007, at 04:06 PM by Paul Badger -
Changed lines 42-43 from:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
to:

Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)

September 08, 2007, at 04:03 PM by Paul Badger -
Changed line 101 from:

Bits that are "bitwise ORed" with 1 are set to 1 so:\\

to:

Bits that are "bitwise ORed" with 1 are set to 1 so:\\

July 17, 2007, at 05:14 PM by Paul Badger -
Changed lines 54-55 from:

Here is the same representation with the variable's bits, replaced with the symbol x

to:

Here is the same representation with the variable's bits replaced with the symbol x

Changed lines 115-116 from:

Here is the same representation with the variables bits, replaced with the symbol x

to:

Here is the same representation with the variables bits replaced with the symbol x

July 17, 2007, at 02:46 PM by Paul Badger -
Added lines 61-63:
 variable unchanged
                     bits cleared
Deleted lines 64-66:

variable unchanged

                     bits cleared
Changed line 123 from:

variable unchanged

to:
 variable unchanged
July 17, 2007, at 02:44 PM by Paul Badger -
Changed line 45 from:
to:

[@

Changed lines 53-54 from:
to:

@]

Changed line 56 from:
to:

[@

Changed line 61 from:
to:

@]

Changed line 91 from:
to:

[@

Changed line 96 from:
to:

@]

Changed line 105 from:
to:

[@

Changed line 114 from:
to:

@]

Changed line 117 from:
to:

[@

Added line 125:

@]

July 17, 2007, at 02:42 PM by Paul Badger -
Changed line 26 from:
to:

[@

Changed lines 31-32 from:
to:

@]

July 17, 2007, at 02:40 PM by Paul Badger -
Changed lines 41-42 from:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number system is zero (hmmm something philosophical here?)
to:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number format is zero (hmmm something philosophical there?)
July 17, 2007, at 02:37 PM by Paul Badger -
Changed lines 41-42 from:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand.
to:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, B00000000 is shown for clarity, but zero in any number system is zero (hmmm something philosophical here?)
July 17, 2007, at 02:20 PM by Paul Badger -
Changed lines 41-42 from:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants.
to:
Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand.
July 17, 2007, at 02:16 PM by Paul Badger -
Changed lines 34-35 from:

myByte & B00000000 = 0;

to:

myByte & B00000000 = 0;

Changed lines 37-38 from:

myByte & B11111111 = myByte;

to:

myByte & B11111111 = myByte;

Note: because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants.
Changed lines 127-128 from:

myByte = 10101010;

to:

myByte = B10101010;

July 17, 2007, at 02:14 PM by Paul Badger -
Changed lines 65-67 from:

myByte &= B1111100 == B10101000;

to:

myByte &= B1111100 == B10101000;@]

July 17, 2007, at 02:13 PM by Paul Badger -
Changed lines 61-67 from:
to:

So if: [@ myByte = 10101010;

myByte &= B1111100 == B10101000;

Changed lines 121-122 from:
to:

So if:

myByte =  10101010;

myByte |= B00000011 == B10101011;

July 17, 2007, at 01:54 PM by Paul Badger -
Changed line 46 from:
   variable unchanged
to:
 variable unchanged
Changed lines 57-58 from:
  variable unchanged
to:

variable unchanged

Changed line 101 from:
   variable unchanged
to:
 variable unchanged
Changed lines 112-113 from:
  variable unchanged
to:

variable unchanged

July 17, 2007, at 01:53 PM by Paul Badger -
Changed lines 32-34 from:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if x is a byte variable,
x & B0 = 0;

to:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if myByte is a byte variable,
myByte & B00000000 = 0;

Changed lines 37-41 from:

x & B11111111 = x;

Consequently - to clear bits 0 & 1 or variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100

   1  0  1  0  1  0  1  0    variable
to:

myByte & B11111111 = myByte;

Consequently - to clear (set to zero) bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100

   1  0  1  0  1  0  1  0    variable  
Changed lines 50-52 from:

Here is the same representation with the variables bits, replaced with the symbol x

to:

Here is the same representation with the variable's bits, replaced with the symbol x

Changed lines 65-66 from:

The compound bitwise OR operator (|=) is often used with a variable and a constant to "set" particular bits in a variable, or force them to the HIGH state (to 1).

to:

The compound bitwise OR operator (|=) is often used with a variable and a constant to "set" (set to 1) particular bits in a variable.

Changed lines 86-88 from:

Bits that are "bitwise ORed" with 0 are unchanged, so if x is a byte variable,
x & B0 = x;

to:

Bits that are "bitwise ORed" with 0 are unchanged, so if myByte is a byte variable,
myByte | B00000000 = myByte;

Changed lines 91-92 from:

x & B11111111 = B11111111;

to:

myByte & B11111111 = B11111111;

July 17, 2007, at 01:44 PM by Paul Badger -
Changed line 32 from:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so,\\

to:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if x is a byte variable,\\

Changed line 35 from:

Bits that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,\\

to:

Bits that are "bitwise ANDed" with 1 are unchanged so, \\

July 16, 2007, at 02:05 PM by Paul Badger -
Changed lines 114-117 from:
  • &(bitwise AND)
  • |(bitwise OR)
  • &&(Boolean AND)
  • ||(Boolean OR)
to:
  • & (bitwise AND)
  • | (bitwise OR)
  • && (Boolean AND)
  • || (Boolean OR)
July 16, 2007, at 02:05 PM by Paul Badger -
Added lines 114-115:
  • &(bitwise AND)
  • |(bitwise OR)
July 16, 2007, at 02:02 PM by Paul Badger -
Changed lines 3-4 from:

The bitwise operators perform their calculations at the bit level of variables. See the bitwise AND (&) and bitwise OR (|) operators for the details of their operation, also the Bitmath Tutorial.

to:

The compound bitwise operators perform their calculations at the bit level of variables. They are often used to clear and set specific bits of a variable.

See the bitwise AND (&) and bitwise OR (|) operators for the details of their operation, and also the Bitmath Tutorial for more information on bitwise operators.

Changed lines 11-12 from:

The compound bitwise AND operator (&=) is often used with a variable and a constant to set particular bits in a variable to the LOW state (to 0). This is also referred to in some programming guides as "clearing" or "resetting" bits.

to:

The compound bitwise AND operator (&=) is often used with a variable and a constant to force particular bits in a variable to the LOW state (to 0). This is often referred to in programming guides as "clearing" or "resetting" bits.

Added lines 32-34:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so,
x & B0 = 0;

Deleted lines 37-39:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so,
x & B0 = 0;

Changed lines 40-122 from:

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

    int a =  92;    // in binary: 0000000001011100
    int b = 101;    // in binary: 0000000001100101
    int c = a & b;  // result:    0000000001000100, or 68 in decimal.

Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 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. See below for an example

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: 0000000001011100
    int b = 101;    // in binary: 0000000001100101
    int c = a | b;  // result:    0000000001111101, or 125 in decimal.

Example Program

A common job for the bitwise AND and OR operators is what programmers call Read-Modify-Write on a port. On microcontrollers, a port is an 8 bit number that represents something about the condition of the pins. Writing to a port controls all of the pins at once.

PORTD is a built-in constant that refers to the output states of digital pins 0,1,2,3,4,5,6,7. If there is 1 in an bit position, then that pin is HIGH. (The pins already need to be set to outputs with the pinMode() command.) So if we write PORTD = B00110001; we have made pins 2,3 & 7 HIGH. One slight hitch here is that we may also have changeed the state of Pins 0 & 1, which are used by the Arduino for serial communications so we may have interfered with serial communication.

     Our algorithm for the program is:
  • Get PORTD and clear out only the bits corresponding to the pins we wish to control (with bitwise AND).
  • Combine the modified PORTD value with the new value for the pins under control (with biwise OR).

int i;     // counter variable
int j;

void setup(){
DDRD = DDRD | B11111100; // set direction bits for pins 2 to 7, leave 0 and 1 untouched (xx | 00 == xx)
// same as pinMode(pin, OUTPUT) for pins 2 to 7
Serial.begin(9600);
}

void loop(){
for (i=0; i<64; i++){

PORTD = PORTD & B00000011;  // clear out bits 2 - 7, leave pins 0 and 1 untouched (xx & 11 == xx)
j = (i << 2);               // shift variable up to pins 2 - 7 - to avoid pins 0 and 1
PORTD = PORTD | j;          // combine the port information with the new information for LED pins
Serial.println(PORTD, BIN); // debug to show masking
delay(100);
   }
}

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. Below is a program to blink digital pin 5.

to:
   1  0  1  0  1  0  1  0    variable
   1  1  1  1  1  1  0  0    mask
   ----------------------
   1  0  1  0  1  0  0  0

   variable unchanged
                     bits cleared

Here is the same representation with the variables bits, replaced with the symbol x

   x  x  x  x  x  x  x  x    variable
   1  1  1  1  1  1  0  0    mask
   ----------------------
   x  x  x  x  x  x  0  0
  variable unchanged
                     bits cleared

compound bitwise OR (|=)

Description

The compound bitwise OR operator (|=) is often used with a variable and a constant to "set" particular bits in a variable, or force them to the HIGH state (to 1).

Syntax:

Changed lines 68-80 from:

// Blink_Pin_5 // demo for Exclusive OR void setup(){ DDRD = DDRD | B00100000; // set digital pin five as OUTPUT Serial.begin(9600); }

void loop(){ PORTD = PORTD ^ B00100000; // invert bit 5 (digital pin 5), leave others untouched delay(100); }@]

to:

x |= y; // equivalent to x = x | y; @]

Parameters

x: a char, int or long variable
y: an integer constant or char, int, or long

Example:

First, a review of the Bitwise OR (|) operator

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

Bits that are "bitwise ORed" with 0 are unchanged, so if x is a byte variable,
x & B0 = x;

Bits that are "bitwise ORed" with 1 are set to 1 so:\\ x & B11111111 = B11111111;

Consequently - to set bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B00000011

   1  0  1  0  1  0  1  0    variable
   0  0  0  0  0  0  1  1    mask
   ----------------------
   1  0  1  0  1  0  1  1

   variable unchanged
                     bits set

Here is the same representation with the variables bits, replaced with the symbol x

   x  x  x  x  x  x  x  x    variable
   0  0  0  0  0  0  1  1    mask
   ----------------------
   x  x  x  x  x  x  1  1
  variable unchanged
                     bits set

July 16, 2007, at 01:41 PM by Paul Badger -
Changed lines 23-31 from:

Bitwise AND (&) operator

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

A review of the bitwise AND (&) operator:

Bits!!! that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,\\

to:

First, a review of the Bitwise AND (&) operator

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

Bits that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,\\

Changed lines 36-39 from:

to:

Consequently - to clear bits 0 & 1 or variable, while leaving the rest of the variable unchanged, use the compound bitwise AND operator (&=) with the constant B11111100

July 16, 2007, at 01:36 PM by Paul Badger -
Changed lines 21-22 from:

Example:

to:

Example:

Bitwise AND (&) operator

    0  0  1  1    operand1
    0  1  0  1    operand2
    ----------
    0  0  0  1    (operand1 & operand2) - returned result
Changed line 31 from:

Bits that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,\\

to:

Bits!!! that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,\\

Changed lines 34-41 from:

    0  0  1  1    operand1
    0  1  0  1    operand2
    ----------
    0  0  0  1    (operand1 & operand2) - returned result
to:

Bits that are "bitwise ANDed" with 0 are cleared to 0 so,
x & B0 = 0;

July 16, 2007, at 01:32 PM by Paul Badger -
Changed lines 1-6 from:

Compound Bitwise AND (&=), Compound Bitwise OR (|=)

The bitwise operators perform their calculations at the bit level of variables. See the Bitwise AND (&) and bitwise OR (|) operators for the details of their operation, also the Bitmath Tutorial.

Compound Bitwise AND (&=)

to:

compound bitwise AND (&=), compound bitwise OR (|=)

The bitwise operators perform their calculations at the bit level of variables. See the bitwise AND (&) and bitwise OR (|) operators for the details of their operation, also the Bitmath Tutorial.

compound bitwise AND (&=)

Changed lines 9-10 from:

The Compound Bitwise AND (&=) is often used with a variable and a constant to set particular bits in a variable to the HIGH state (to 1).

to:

The compound bitwise AND operator (&=) is often used with a variable and a constant to set particular bits in a variable to the LOW state (to 0). This is also referred to in some programming guides as "clearing" or "resetting" bits.

Changed lines 23-26 from:

 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:
to:

A review of the bitwise AND (&) operator: Bits that are "bitwise ANDed" with 1 are unchanged so, if x is a byte variable,
x & B11111111 = x;

July 16, 2007, at 01:23 PM by Paul Badger -
Changed lines 3-4 from:
to:

The bitwise operators perform their calculations at the bit level of variables. See the Bitwise AND (&) and bitwise OR (|) operators for the details of their operation, also the Bitmath Tutorial.

July 16, 2007, at 01:22 PM by Paul Badger -
Changed line 17 from:

x: a char, int or long variable\\\

to:

x: a char, int or long variable\\

July 16, 2007, at 01:22 PM by Paul Badger -
Changed line 17 from:

x: a char, int or long variable

to:

x: a char, int or long variable\\\

July 16, 2007, at 01:21 PM by Paul Badger -
Added lines 7-10:

Description

The Compound Bitwise AND (&=) is often used with a variable and a constant to set particular bits in a variable to the HIGH state (to 1).

Changed lines 12-14 from:

x &= y;

to:
x &= y;   // equivalent to x = x & y; 
Changed lines 18-25 from:

 bitwise AND (&= operator in C++ is equivalent to the expre

to:

y: an integer constant or char, int, or long

Example:

July 16, 2007, at 01:13 PM by Paul Badger -
Changed lines 3-16 from:

The bitwise operators perform their calculations at the bit level of variables. See the Bitwise AND (&) and bitwise OR |

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:

to:

The bitwise operators perform their calculations at the bit level of variables. See the Bitwise AND (&) and bitwise OR (|) operators for the details of their operation, also the [[http://www.arduino.cc/playground/Code/BitMath | Bitmath Tutorial.

Compound Bitwise AND (&=)

Syntax:

x &= y;

Parameters

x: a char, int or long variable

 bitwise AND (&= operator in C++ is equivalent to the expre

 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:
July 16, 2007, at 01:03 PM by Paul Badger -
Added lines 1-115:

Compound Bitwise AND (&=), Compound Bitwise OR (|=)

The bitwise operators perform their calculations at the bit level of variables. See the Bitwise AND (&) and bitwise OR |

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 Arduino, the type int is a 16-bit value, so using & between two int expressions causes 16 simultaneous AND operations to occur. In a code fragment like:

    int a =  92;    // in binary: 0000000001011100
    int b = 101;    // in binary: 0000000001100101
    int c = a & b;  // result:    0000000001000100, or 68 in decimal.

Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 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. See below for an example

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: 0000000001011100
    int b = 101;    // in binary: 0000000001100101
    int c = a | b;  // result:    0000000001111101, or 125 in decimal.

Example Program

A common job for the bitwise AND and OR operators is what programmers call Read-Modify-Write on a port. On microcontrollers, a port is an 8 bit number that represents something about the condition of the pins. Writing to a port controls all of the pins at once.

PORTD is a built-in constant that refers to the output states of digital pins 0,1,2,3,4,5,6,7. If there is 1 in an bit position, then that pin is HIGH. (The pins already need to be set to outputs with the pinMode() command.) So if we write PORTD = B00110001; we have made pins 2,3 & 7 HIGH. One slight hitch here is that we may also have changeed the state of Pins 0 & 1, which are used by the Arduino for serial communications so we may have interfered with serial communication.

     Our algorithm for the program is:
  • Get PORTD and clear out only the bits corresponding to the pins we wish to control (with bitwise AND).
  • Combine the modified PORTD value with the new value for the pins under control (with biwise OR).

int i;     // counter variable
int j;

void setup(){
DDRD = DDRD | B11111100; // set direction bits for pins 2 to 7, leave 0 and 1 untouched (xx | 00 == xx)
// same as pinMode(pin, OUTPUT) for pins 2 to 7
Serial.begin(9600);
}

void loop(){
for (i=0; i<64; i++){

PORTD = PORTD & B00000011;  // clear out bits 2 - 7, leave pins 0 and 1 untouched (xx & 11 == xx)
j = (i << 2);               // shift variable up to pins 2 - 7 - to avoid pins 0 and 1
PORTD = PORTD | j;          // combine the port information with the new information for LED pins
Serial.println(PORTD, BIN); // debug to show masking
delay(100);
   }
}

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. Below is a program to blink digital pin 5.

// Blink_Pin_5
// demo for Exclusive OR
void setup(){
DDRD = DDRD | B00100000; // set digital pin five as OUTPUT 
Serial.begin(9600);
}

void loop(){
PORTD = PORTD ^ B00100000;  // invert bit 5 (digital pin 5), leave others untouched
delay(100);
}

See Also

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

Share