Reference.Bitwise History

Show minor edits - Show changes to markup

April 18, 2007, at 03:36 AM by Paul Badger -
Deleted line 0:
Added lines 29-36:
    0  1    operand1

   ----------
    1  0   ~ operand1

April 18, 2007, at 03:34 AM by Paul Badger -
Changed lines 1-12 from:

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

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:

to:

Bitwise XOR (^) & Bitwise Not (~)

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:

Deleted lines 11-82:
    0  1  0  0    (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:

    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    1  1  0  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.

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).

// program to demonstrate writing to ports & bitmasking
// Paul Badger 2007
// set some LED's (w. series resistors) up on pins 2 to 7
// and watch the program count in binary

int i;     // counter variable
int j;

void setup(){
DDRD = DDRD & 11111100; // set direction bits for pins 2 to 7
// same as pinMode(pin, OUTPUT) for pins 2 to 7
}

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

PORTD = PORTD & B00000011;  // mask out bits 2 - 7
Serial.println(PORTD, BIN); // debug to show masking
j = (i << 2);               // shift variable up to pins 2 - 7
PORTD = PORTD | j;          // combine the port information with the new information for LED pins
Serial.println(PORTD, BIN); // debug to show masking
   }
} 




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:

    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
April 17, 2007, at 12:28 AM by Paul Badger -
Changed lines 85-89 from:
    0 ^ 0 == 0
    0 ^ 1 == 1
    1 ^ 0 == 1
    1 ^ 1 == 0
to:
    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    1  0  0  1    (operand1 ^ operand2) - returned result
April 17, 2007, at 12:27 AM by Paul Badger -
Changed lines 49-53 from:

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).
  • Get the new value to pins and mask only the pins we wish to control
  • Combine the PORTD value with the new value for the pins under control
to:
     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).

[@// program to demonstrate writing to ports & bitmasking // Paul Badger 2007 // set some LED's (w. series resistors) up on pins 2 to 7 // and watch the program count in binary

int i; // counter variable int j;

Changed lines 63-67 from:

// same as pinMode(pin, OUTPUT) for every pin

to:

// same as pinMode(pin, OUTPUT) for pins 2 to 7 }

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

PORTD = PORTD & B00000011; // mask out bits 2 - 7 Serial.println(PORTD, BIN); // debug to show masking j = (i << 2); // shift variable up to pins 2 - 7 PORTD = PORTD | j; // combine the port information with the new information for LED pins Serial.println(PORTD, BIN); // debug to show masking

   }

}

@]

April 17, 2007, at 12:05 AM by Paul Badger -
Changed lines 36-37 from:
    1  1  0  1    (operand1 & operand2) - returned result
to:
    1  1  0  1    (operand1 | operand2) - returned result
Changed lines 50-55 from:
  • Get PORTD and clear out only the bits corresponding to the pins we wish to control.

to:
  • Get PORTD and clear out only the bits corresponding to the pins we wish to control (with bitwise AND).
  • Get the new value to pins and mask only the pins we wish to control
  • Combine the PORTD value with the new value for the pins under control

void setup(){ DDRD = DDRD & 11111100; // set direction bits for pins 2 to 7 // same as pinMode(pin, OUTPUT) for every pin

April 16, 2007, at 11:53 PM by Paul Badger -
Changed lines 44-45 from:

A common job for the bitwise AND and OR operators is what programmers call Read-Modify-Write on a port. In microcontrollers a port is an 8 bit number that represents something about the condition of the pins. PORTD is a constant that refers to the output states of . In this

to:

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.

April 16, 2007, at 11:38 PM by Paul Badger -
Changed lines 13-17 from:
    0 & 0 == 0
    0 & 1 == 0
    1 & 0 == 0
    1 & 1 == 1
to:
    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    0  1  0  0    (operand1 & operand2) - returned result
Changed lines 26-33 from:

One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. For example, if you wanted to access the least significant bit in a variable x, and store the bit in another variable y, you could use the following code:

    int x = 5;       // binary: 101
    int y = x & 1;   // now y == 1
    x = 4;           // binary: 100
    y = x & 1;       // now y == 0

to:

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

Changed lines 33-37 from:
    0 | 0 == 0
    0 | 1 == 1
    1 | 0 == 1
    1 | 1 == 1
to:
    1  1  0  0    operand1
    0  1  0  1    operand2
    ----------
    1  1  0  1    (operand1 & operand2) - returned result
Changed lines 44-47 from:

Bitwise OR is often used to make sure that a given bit is turned on (set to 1) in a given expression. For example, to copy the bits from a into b, while making sure the lowest bit is set to 1, use the following code:

    b = a | 1;
to:

A common job for the bitwise AND and OR operators is what programmers call Read-Modify-Write on a port. In microcontrollers a port is an 8 bit number that represents something about the condition of the pins. PORTD is a constant that refers to the output states of . In this

April 16, 2007, at 05:56 PM by Paul Badger -
Changed lines 88-89 from:
to:
April 16, 2007, at 07:29 AM by Paul Badger -
Added lines 87-89:
April 16, 2007, at 07:24 AM by Paul Badger -
Changed lines 3-4 from:

The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. An excellent tutorial on bitwise math may be found here.

to:

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.

April 16, 2007, at 07:18 AM by Paul Badger -
Changed lines 9-10 from:

Bitwise AND

to:

Bitwise AND (&)

Changed lines 34-35 from:

Bitwise OR

to:

Bitwise OR (|)

Changed lines 53-54 from:

Bitwise XOR

to:

Bitwise XOR (^)

Changed lines 74-75 from:

Bitwise NOT

to:

Bitwise NOT (~)

April 16, 2007, at 07:14 AM by Paul Badger -
Changed lines 9-19 from:

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 == 0
    0 | 1 == 1
    1 | 0 == 1
    1 | 1 == 1

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

to:

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 == 0
    0 & 1 == 0
    1 & 0 == 0
    1 & 1 == 1

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:

Added lines 22-46:
    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. For example, if you wanted to access the least significant bit in a variable x, and store the bit in another variable y, you could use the following code:

    int x = 5;       // binary: 101
    int y = x & 1;   // now y == 1
    x = 4;           // binary: 100
    y = x & 1;       // now y == 0

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 == 0
    0 | 1 == 1
    1 | 0 == 1
    1 | 1 == 1

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
April 16, 2007, at 07:11 AM by Paul Badger -
Changed lines 9-19 from:

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 == 0
    0 & 1 == 0
    1 & 0 == 0
    1 & 1 == 1

Bitwise OR

to:

Bitwise OR

Changed lines 18-19 from:
 Bitwise XOR
to:

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.

Bitwise OR is often used to make sure that a given bit is turned on (set to 1) in a given expression. For example, to copy the bits from a into b, while making sure the lowest bit is set to 1, use the following code:

    b = a | 1;

Bitwise XOR

Changed lines 37-39 from:
 Bitwise NOT
to:

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 while leaving others alone. For example:

    y = x ^ 1;   // toggle the lowest bit in x, and store the result in y.

Bitwise NOT

Changed lines 56-61 from:
to:

You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two's complement. For more information, see the Wikipedia article on two's complement.

As an aside, it is interesting to note that for any integer x, ~x is the same as -x-1.

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

April 16, 2007, at 07:03 AM by Paul Badger -
Added lines 9-44:

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 == 0
    0 & 1 == 0
    1 & 0 == 0
    1 & 1 == 1

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 == 0
    0 | 1 == 1
    1 | 0 == 1
    1 | 1 == 1

 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 == 0
    0 ^ 1 == 1
    1 ^ 0 == 1
    1 ^ 1 == 0

 Bitwise NOT

The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0. For example:

    int a = 103;    // binary:  0000000001100111
    int b = ~a;     // binary:  1111111110011000 = -104

April 16, 2007, at 07:00 AM by Paul Badger -
Changed lines 3-8 from:

The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. An excellent tutorial on bitwise math may be found [[

to:

The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. An excellent tutorial on bitwise math 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.

April 16, 2007, at 06:58 AM by Paul Badger -
Added lines 1-3:

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

The bitwise operators perform their calculations at the bit level of variables. They help solve a wide range of common programming problems. An excellent tutorial on bitwise math may be found [[

Share