Tels <perl_dummy@bloodgate.com> wrote
> Math::BigInt's testsuite is failing, because at one point $caught & 1 == 0
> is not properly working inside Perl:
>
> bash-2.03# perl -le '$a = 0; print "== 0" if $a & 1 == 0; print "$a == 0"
> if $a == 0; print "!= 0" if $a & 1 != 0'
> 0 == 0
>
> It should print "== 0" and "0 == 0". $a & 1 must be either 0 or not 0.
No it shouldn't. See perlop.pod:
nonassoc < > <= >= lt gt le ge
>>>>>>> nonassoc == != <=> eq ne cmp
>>>>>>> left &
left | ^
== binds more tightly than & and |.
This one frequently catches me; I naturally feel that & and | ought to
have very high precedence. So I now have a rule *always* to bracket
the & ^ | operators when used in a more complex expression.
IIRC this anomalous precedence is there for C compatibility.
In general, I dislike unnecessary repetition in the pods, but this case
bites sufficiently often to deserve a mention. (And yes, the examples
are poor - I can't think of any which are both simple and sensible.)
Mike Guy
--- ./pod/perlop.pod.orig Tue Mar 5 01:46:16 2002
+++ ./pod/perlop.pod Thu Sep 26 13:14:43 2002
@@ -336,16 +336,26 @@
=head2 Bitwise And
-Binary "&" returns its operators ANDed together bit by bit.
+Binary "&" returns its operaands ANDed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
+Note that "&" has lower priority than relational operators, so for example
+the brackets are essential in a test like
+
+ print "Even\n" if ($x & 1) == 0;
+
=head2 Bitwise Or and Exclusive Or
-Binary "|" returns its operators ORed together bit by bit.
+Binary "|" returns its operaands ORed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
Binary "^" returns its operators XORed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
+
+Note that "|" and "^" have lower priority than relational operators, so
+for example the brackets are essential in a test like
+
+ print "false\n" if (8 | 2) != 10;
=head2 C-style Logical And
End of patch
Thread Next