Michael G Schwern wrote:
>There's an old, but sadly true, joke about some bizarre feature of C and why
>it's there.  Turns out waaaaaaaaaaaay back in the beginning the bizarreness of
>the feature was pointed out and a better way to do it given.  At the time the
>response was "we can't possibly change that, there must be *a dozen users* of
>C by now!"

That would be the surprisingly low precedence of the bitwise operators
| and &.  The earliest versions of C lacked the logical operators ||
and &&, and one would use the bitwise operators for logic operations.
There was a special rule that bitwise operators at the top level of the
controlling expression in an if statement would have the short-circuiting
behaviour of the modern || and &&.  Because that was the main use of |
and &, their precedence was chosen for logic operations, and so had to
be lower than the comparison operators.

When the two uses of these operators were spearated, inventing ||
and && for the logic behaviour, it was realised that | and &, being
now purely non-short-circuiting bitwise operators, ought to have much
higher precedence.  But by this time there were hundreds of kilobytes
of code written for the old precedence rules, and it would have been an
awful bother to change it all.  So the bitwise and logical operators were
minimally separated to adjacent precedence levels, causing confusion to
(so far) two generations of programmers using C and its derivatives.
Perl is one of the languages that perpetuates the confusion.

-zefram

Reply via email to