Why is there no ^^ operator in C/C++?

前端 未结 7 1931
孤街浪徒
孤街浪徒 2020-12-16 13:31

& has &&. | has ||. Why doesn\'t ^ have ^^?

I understand that it wouldn\'t

7条回答
  •  佛祖请我去吃肉
    2020-12-16 14:02

    I can't say what was in the heads of Kernighan and Ritchie when they invented C, but you made a brief reference to "wouldn't be short-circuiting", and I'm guessing that's the reason: It's not possible to implement it consistently. You can't short-circuit XOR like you can AND and OR, so ^^ could not fully parallel && and ||. So the authors might well have decided that making an operation that sort of kind of looks like its parallel to the others but isn't quite would be worse than not having it at all.

    Personally, the main reason I use && and || is for the short-circuit rather than the non-bitwise. Actually I very rarely use the bitwise operators at all.

提交回复
热议问题