bitwise XOR (^)

Using this function only returns 0. For example

where the 3 variables are either a byte or an unsigned int

difference = value ^ oldValue;

The line compiles correctly, but difference is always 0 even if value != oldValue.

Am I not using it right or is there some bug in the compiler?

There are other ways to compare bits, but this is the easiest to understand.

Using this function only returns 0.

You haven't posted the code of your function.

Please remember to use code tags.

How are you inspecting the result? Provide the context.

You're doing something else wrong. bitwise xor works fine, I use it often.

This sketch demonstrates that it works:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int val1=random(32767);
  int val2=random(32767);
  int difference=val1^val2;
  Serial.print("val1=");
  Serial.println(val1);
  Serial.print("val2=");
  Serial.println(val2);
  Serial.print("difference=");
  Serial.println(difference);
  delay(500);
}

There may be a misunderstanding...

There are other ways to compare bits, but this is the easiest to understand.

XOR isn’t a comparison operator...
You can use == or !=

lastchancename:
There may be a misunderstanding...
XOR isn’t a comparison operator...
You can use == or !=

XOR effectively is a bitwise comparison operator, which compares the corresponding bit positions in two values and flags the positions that differ.

Judging by the OP's wording, that is exactly how they are trying to use it.

Montmorency:
XOR effectively is a bitwise comparison operator, which compares the corresponding bit positions in two values and flags the bits that differ.

Judging by the OP’s wording, that is exactly how they are trying to use it.

Basically it’s a bitwise !=.

OP, you are doing something wrong in the code you haven’t posted. Post your actual complete code, not a reconstructed snippet.

hacketet:
difference = value ^ oldValue;

The line compiles correctly, but difference is always 0 even if value != oldValue.

No it isn't. I mean: that's all that can be said here with no other context. If, when that line is executed, value != oldValue, the exclusive or will definitely be nonzero. The usual cause of this is

  • you are not actually setting value
  • you are setting oldValue equal to value by using '=' when you should be using '=='
  • you are setting oldvalue = value before the comparison when you should be doing it after the comparison
  • you have not understood variable scope and lifetime, and you are recreating oldvalue each time you loop
  • oldvalue really is == value. Possibly because you are polling some input faster than you need to
  • you are resetting or ignoring the value of difference after it is set by this line of code.

But my money is on variable hiding. You have misunderstood variable scope, or have copy-pasted the declaration of the variable.

int oldValue;
loop() {
  int value = oldValue;

  if(it's time to poll the value again) {
    /// this won't work -this 'value' is a new, different variable to the one outside
    int value = read_the_value();
  }
  int difference = oldValue ^ value;
  if(difference) println(difference);
  oldValue = value;
}
int oldValue;
loop() {
  int value = oldValue;

  if(it's time to poll the value again) {
    /// this might work - put the value into the existing value variable 
    value = read_the_value();
  }
  int difference = oldValue ^ value;
  if(difference) println(difference);
  oldValue = value;
}