&

Difference between version 6 and 7 - Previous - Next
[expr] bit-wise "and" operator, dual of [|]

Arguments must be integers, result is an integer.

Bit ''n'' of the result is 1 if bit ''n'' of each argument is 1. Otherwise, bit ''n'' of the result is 0.

For negative arguments, we use the extended definition of [~] that '''[~]$a[==]-1-$a'''.
There are then the following cases:

%| Case | Result |%
&| $a[>=]0,$b>=0 | Ordinary bitwise '''&''' |&
&| $a>=0,$b[<]0 | `$a&$b == $a & ~(~$b)` ''Contrapositive law'' <<br>> `$a&$b == $a & ~ (-1-$b)`  ''Extended definition of [~]'' <<br>> Since -1-$b is positive, $a & ~(-1-$b) can be evaluated in bitwise fashion. |&
&| $a<0,$b>=0 | Commute to ($b & $a) and use the calculation above |&
&| $a<0,$b<0 | `$a&$b == ~ (~$a <<pipe>> ~$b)` ''De Morgan's Law'' <<br>> `$a&$b == ~ ((-1-$a) <<pipe>> (-1-$b))` ''Extended definition of [~]'' <<br>> `$a&$b == -1-((-1-$a) <<pipe>> (-1-$b))` ''Extended definition of [~]'' <<br>> Since [-]1-$a and -1-$b are both positive, the expression ((-1-$a) [<<pipe>>] (-1-$b)) can be evaluated in the ordinary bitwise fashion. |&

For logical/short-cut "and" use the [&&] operator.
----[AMG]: Be wary of the precedence of this operator!  Like [&&], it has lower precedence than the comparison operators.  For more information on why, read the section "Neonatal C" found at [http://cm.bell-labs.com/cm/cs/who/dmr/chist.html].

%|Expression   |Result|Comment                       |%
&|expr 5&2==2  |1     |expression without parentheses|&
&|expr 5&(2==2)|1     |actual behavior of Tcl and [C]|&
&|expr (5&2)==2|0     |naively expected behavior     |&

----
!!!!!!
%| [Category Operator] |%
!!!!!!