^ (Bitwise xor)

From Microduino Wiki
Revision as of 03:11, 11 August 2016 by Fengfeng (talk) (Created page with "*'''Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)''' Bitwise xor(^) There is a uncommon operator in C++ named bitwise xor , which is represented by ‘^'. This operat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  • Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)

Bitwise xor(^)

There is a uncommon operator in C++ named bitwise xor , which is represented by ‘^'. This operator is very similar to bitwise or(|), and the difference is if both the two bits are 1, the result is 0:

0 0 1 1 Operand 1 0 1 0 1 Operand 2


0 1 1 0(Operand 1 ^ operand 2) - The returned result Another explanation of bitwise xor is if the two values of the bits are same, the result is 0, otherwise it is 1.

The following is a simple code example:

    int x = 12;     // Binary: 1100
    int y = 10;     // Binary: 1010
    int z = x ^ y;  // Binary: 0110,  or decimal 6
// Blink_Pin_5
//Demonstrate “xor”
void setup(){
DDRD = DDRD | B00100000; //Set digital pin 5 as output.
serial.begin(9600);
}
 
void loop ()   {
PORTD = PORTD ^ B00100000;  // Reverse the fifth bit(digital pin 5), and keep others unchanged. 
delay(100);
}


[Return to Arduino Syntax Manual]