Idiomas copiaram isso de C, e para C, Dennis Ritchie explica que inicialmente, em B (e talvez no início C), havia apenas uma forma &
que, dependendo do contexto, era bit a bit e / ou lógica. Posteriormente, cada função obteve seu operador: &
para o bit a bit e &&
para o um lógico. Então ele continua
Their tardy introduction explains an infelicity of C's precedence rules. In B one writes
if (a == b & c) ...
to check whether
a
equalsb
andc
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 writeif ((a & mask) == b) ...
where the inner parentheses are required but easily forgotten.