There are both historical and practical reasons why there is no ^^ operator.

本文探讨了C语言中缺乏逻辑异或运算符的原因。从实用性和历史角度出发,解释了逻辑异或运算符在表达式短路评估方面的不足,并回顾了C语言及其前身如何处理位运算和逻辑运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://c-faq.com/misc/xor.dmr.html

C Programming FAQs: Frequently Asked Questions"

http://returnc.com/archives/2835.html

https://myreadmark.wordpress.com/2014/06/04/cpu-and-memory/


Back in July of 1995,Adam Moscowitz posed this question to Dennis Ritchie:

I teach C programming classes, and I'm often asked why C has a bitwiseexclusive-OR operator but not a logical one. Is this simply anhistorical accident, or was there a specific reason it was omitted?Also, is there any reason why the C9x committee shouldn't consideradding it to the standard?

Dennis replied:

I've often been asked this in person, and it comes up on netnews too.Since I formulated a mildly coherent answer, I'm sending it alongto Steve in case a suitably edited version is suitable for the C FAQ.


Answer:

There are both historical and practical reasons why there is no ^^ operator.

The practical is: there's not much use for the operator. The main pointof && and || is to take advantage of their short-circuit evaluation notonly for efficiency reasons, but more often for expressiveness and correctness.For example, in

	if (cond1() && cond2()) ...
it is often important that cond1() is done first, because if it's falsecond2() may not be well defined, or becausecond1() is cheap andcond2() is expensive. Syntax to handle the situation has made it into lotsof languages; compare Ada's `and then'.

By contrast, an ^^ operator would always force evaluation of both armsof the expression, so there's no efficiency gain. Furthermore, situationsin which^^ is really called for are pretty rare, though examples canbe created. These situations get rarer and stranger as youstack up the operator--

	if (cond1() ^^ cond2() ^^ cond3() ^^ ...) ...
does the consequent exactly when an odd number of thecondx()s are true. By contrast, the&& and|| analogs remainfairly plausible and useful.

Historical: C's predecessors (B and BCPL) had only the bitwiseversions of | & ^. They also had a special rule, namely thatin an statement like

	if (a & b) ...
the `&', being at the `top level' of the expression occurring in`truth-value context' (inside theif()) was interpreted justlike C's&&. Similarly for|. But not so for^.

One of the early bits of C evolution was the creation of separate&& and|| operators. This was better than the special rule, which was hardto explain.

In other words, the whole question arises because of the particular methodof symmetry-breaking that C chose. Suppose I had reacted tothe situation of BCPL and B taking their notion of

	if (a & b) ...

and had people say

	if (a) andif (b) ...
(and similarly with `orif'). One can make a case that this kind of syntax isbetter than&& and||. If it had happened, would people be asking for `xorif'?Probably not.

My guess is that &,&&; |,||; ^,^^ is a false symmetry. But it'sone that people seem to want, and, though it's not much help,adding it wouldn't do much harm.

Dennis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值