[Issue 4077] Bugs caused by bitwise operator precedence

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 7 02:02:21 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4077


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug at yahoo.com.au


--- Comment #9 from Don <clugdbug at yahoo.com.au> 2010-05-07 02:01:48 PDT ---
(In reply to comment #5)
> The precedence of bitwise operators is indeed counter-intuitive.  Presumably
> there's a reason C defined them this way.

Yes, there is -- backwards compatibility with the B language!!!

Denis Ritchie says (http://cm.bell-labs.com/cm/cs/who/dmr/chist.html):
--------------------
At the suggestion of Alan Snyder, I introduced the && and || operators to make
the mechanism [[short circuit evaluation]] more explicit.

Their tardy introduction explains an infelicity of C's precedence rules. In B
one writes

    if (a==b & c) ...

to check whether a equals b and c is non-zero; in such a conditional expression
it is better that & have lower precedence than ==. In converting from B to C,
one wants to replace & by && in such a statement; to make the conversion less
painful, we decided to keep the precedence of the & operator the same relative
to ==, and merely split the precedence of && slightly from &. Today, it seems
that it would have been preferable to move the relative precedences of & and
==, and thereby simplify a common C idiom: to test a masked value against
another value, one must write

    if ((a&mask) == b) ...

where the inner parentheses are required but easily forgotten. 
-----------------------------------

So C did it for an unbelievably silly reason (there was hardly any B code in
existence). Note that Ritchie says it is "easily forgotten".
We should definitely fix this ridiculous precedence. (IMHO it was very sloppy
that ANSI C didn't make (a&b == c) an error).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list