Because CL specifies bitwise operations treat integers as being in two's complement, if your bignums use a signed magnitude representation they are nontrivial. You will have to test signs while doing your arithmetic operations, and do different things depending. Generally, keep in mind that for integer x, (- x)
= (lognot (1- x))
= (1+ (lognot x))
, and that the number of bignum elements in the output may exceed those in the input. (Consider (logand -2 -3)
: It's -4, and 4 needs one more bit to represent than 2 or 3 do.)
For bitwise operations of two operands, you can understand the sign of the result by thinking of the infinite left bits. For example, if logior
is given one positive and one negative operand, the result is negative, because for each of these infinitely many bits, (logior 0 1)
= 1.
Here are some particular identities that are useful, derived from basic boolean algebra plus the above. `(- (