⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xor.dmr.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- &lt;199507200321.UAA09304@mail.eskimo.com&gt; --><!-- Mirrored from c-faq.com/misc/xor.dmr.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:55 GMT --><head><title>dmr on XOR</title></head><body><p>Back in July of 1995,Adam Moscowitz posed this question to Dennis Ritchie:<p><blockquote>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?</blockquote><p>Dennis replied:<p>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.<p><hr><p>Answer:<p>There are both historical and practical reasons why there is no <TT>^^</TT> operator.<p>The practical is: there's not much use for the operator.  The main pointof <TT>&amp;&amp;</TT> and <TT>||</TT> is to take advantage of their short-circuit evaluation notonly for efficiency reasons, but more often for expressiveness and correctness.For example, in<pre>	if (cond1() &amp;&amp; cond2()) ...</pre>it is often important that <TT>cond1()</TT> is done first, because if it's false<TT>cond2()</TT> may not be well defined, or because <TT>cond1()</TT> is cheap and<TT>cond2()</TT> is expensive.  Syntax to handle the situation has made it into lotsof languages; compare Ada's `and then'.<p>By contrast, an <TT>^^</TT> operator would always force evaluation of both armsof the expression, so there's no efficiency gain.  Furthermore, situationsin which <TT>^^</TT> is really called for are pretty rare, though examples canbe created.  These situations get rarer and stranger as youstack up the operator--<pre>	if (cond1() ^^ cond2() ^^ cond3() ^^ ...) ...</pre>does the consequent exactly when an odd number of the<TT>condx()</TT>s are true.   By contrast, the <TT>&amp;&amp;</TT> and <TT>||</TT> analogs remainfairly plausible and useful.<p>Historical:  C's predecessors (B and BCPL) had only the bitwiseversions of <TT>|</TT> <TT>&amp;</TT> <TT>^</TT>.   They also had a special rule, namely thatin an statement like<pre>	if (a &amp; b) ...</pre>the `<TT>&amp;</TT>', being at the `top level' of the expression occurring  in`truth-value context' (inside the <TT>if()</TT>)  was interpreted justlike C's <TT>&amp;&amp;</TT>.  Similarly for <TT>|</TT>. But not so for <TT>^</TT>.<p>One of the early bits of C evolution was the creation of separate<TT>&amp;&amp;</TT> and <TT>||</TT> operators.  This was better than the special rule, which was hardto explain.<p>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<p><pre>	if (a &amp; b) ...</pre><p>and had people say<p><pre>	if (a) andif (b) ...</pre>(and similarly with `<TT>orif</TT>').  One can make a case that this kind of syntax isbetter than <TT>&amp;&amp;</TT> and <TT>||</TT>.  If it had happened, would people be asking for `<TT>xorif</TT>'?Probably not.<p>My guess is that <TT>&amp;</TT>,<TT>&amp;&amp;</TT>; <TT>|</TT>,<TT>||</TT>; <TT>^</TT>,<TT>^^</TT> 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.<p><blockquote>	Dennis</blockquote></body><!-- Mirrored from c-faq.com/misc/xor.dmr.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:55 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -