Version 1 of &

Updated 2007-11-05 14:43:54 by kbk

expr bit-wise "and" operator

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 & ~ (-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
                     == ~ ((-1-$a) | (-1-$b))  Extended definition of [~]
                     == -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.