Does the bitwise operator ^=
create a temporary variable in memory when it's used?
so for a example if I have:
a ^= b;
Does it create a copy of a
in memory, then check against it and then assign? Or does it just straight up check and then assign without creating a temporary variable?
This is a compiler specific question, but I tried it with g++ -O2
and clang++ -O2
. It compiled this:
int main (int argc, char** argv) {
int a = argc, b = argc * 3;
a ^= b;
return a;
}
to
leal (%rdi,%rdi,2), %eax
xorl %edi, %eax
ret
The a ^= b
part responds to the xorl
line, which, as you can see, is a single instruction. So gcc did not create and then assign a new variable, but just left the operation directly to the CPU.
Note that you should look at this purely because you find it interesting. From a performance point of view, you should not care about such things and leave it to your compiler. It is pretty good in optimizing this stuff, so focus your time and knowledge on writing correct and readable code!