&有&&。|有||。为什么^没有^^?
我知道它不会短路,但它会有不同的语义。在 C 中,true实际上是任何非零值。按位异或并不总是与逻辑异或相同:
int a=strcmp(str1,str2);// evaluates to 1, which is "true"
int b=strcmp(str1,str3);// evaluates to 2, which is also "true"
int c=a ^^ b; // this would be false, since true ^ true = false
int d=a ^ b; //oops, this is true again, it is 3 (^ is bitwise)
既然你不能总是依赖一个真正的价值是1or -1,那么^^运算符不是很有帮助吗?我经常不得不做这样奇怪的事情:
if(!!a ^ !!b) // looks strange