🌟2's complement

-x = ~x + 1 (invert bits, then add 1).

JSstatementexpressionoperatorarithmeticbitwise ⟩ 2's complement

circle-check
chevron-right💡 proof: ~(x ± M) ≡ ~x , ~~(x ± M) ≡ ~~x (mod M)hashtag
// 1. ~(x ± M)  ≡  ~x
// proof:
~(x ± M) = -(x ± M) - 1        // definition
         = -x ± M -1
-x - 1 (mod M)      // modular arithmetic
         = ~x                  // definition
// 2. ~~(x ± M)  ≡  ~~x
// proof:
~~(x ± M) = ~(~(x ± M))        // `~` is right-to-left associative
~(~x)              // by proof 1.
          = ~~x                // `~` is right-to-left associative
circle-check
Drawing
signed integer additon is a modular arithmetic
Drawing
integer addition may overflow/underflow.

Last updated