Edit
The short answer: You cannot perform bitwise operations on pointers. Unfortunately, you cannot perform bitwise operations on floats, either! Don't use the XOR swap for float
values.
Original
The commenters gave you the short answer. Now for my take on the long answer —
Bitwise operations such as ^
are defined for integers. However, a pointer is not necessarily an integer. On real-mode x86, a pointer includes, or is in some way related to, two integers: a segment and an offset (additional info).
Worse, those two integers overlap, so changes to one also change the other. Therefore, there is no single, unambiguous way to define ^
or other bitwise operations for pointers.
Lots of code does assume that pointers can be treated as integers, since segmented addressing is less common than it used to be. However, the C standard still has to support less-common architectures, so does not define bitwise operations on pointers.
Rest of edit
I see that the example you linked uses pointers. That is because, in C, you have to use pointers to pass values back to the caller of a function through parameters. The code you linked is:
void xorSwap (int *x, int *y) {
if (x != y) {
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}
(enwiki, CC-BY-SA 3.0). You could call that in your context as
if(x[i]!=x[j]) xorSwap(&x[i], &x[j]);
if x
were an array of int
s, and it would swap the contents of the array elements, as I think you expect. When you are using XOR swap directly, rather than via a function, you do not need to use pointers at all. For example:
if(x[i]!=x[j]) {
x[i] ^= x[j];
x[j] ^= x[i];
x[i] ^= x[j];
}
should work, again, provided that x
is int
and not float
.