Hello, I’m running a program that someone else written. It runs smoothly but I want to understand what the code does.
I have the following snippet:
for (int i = n-1; i > 0; i–) {
int j = rand()%(i+1);
image.pixels[j]^=image.pixels[i];
image.pixels[i]^=image.pixels[j];
image.pixels[j]^=image.pixels[i];
}
what does the “^=” mean? I haven’t found that in the Reference.
Regards
Hans
1 Like
That’s a bitwise operator. Specifically it’s a bitwise XOR operator.
There’s also a reassignment operator in there, so this line:
image.pixels[j] ^= image.pixels[i];
Is really doing this:
image.pixels[j] = image.pixels[j] ^ image.pixels[i];
There are a ton of resources on google, but basically, bitwise operators are like the operators you’re used to (+
or -
for example) but they work on the binary representation of the numbers you’re using.
This page has a pretty good explanation of bitwise operators.
3 Likes
Thanks a lot for a quick and understandable response.
Hans