I was reading through some C code written online and came across the following line:
if(!(array[index] ^ array[index - 1]))
The ^ operator is a bitwise XOR, so I read this line to say that it will return true if "The array value at index is not different to the value at the previous index." Simplified, I read this as "If the array value at the index is the same as the one at the previous index."
When I read it like that, it seems like an overcomplicated way to write:
if(array[index] == array[index - 1])
Are these expressions the same? If not, then why? If I'm not misreading it, the best explanation I have is that since this code is involved with interrupts on clock signals it needs to be quick. Maybe a bitwise operation is faster than whatever goes on behind-the-scenes with ==
?