Version 5 of &

Updated 2007-11-10 17:52:10 by dkf

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
$a&$b == $a & ~ (-1-$b) Extended definition of ~
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 | ~$b) De Morgan's Law
$a&$b == ~ ((-1-$a) | (-1-$b)) Extended definition of ~
$a&$b == -1-((-1-$a) | (-1-$b)) Extended definition of ~
Since -1-$a and -1-$b are both positive, the expression ((-1-$a) | (-1-$b)) can be evaluated in the ordinary bitwise fashion.

For logical/short-cut "and" use the && operator.