๐ŸŒŸ2's complement

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

JS โŸฉ statement โŸฉ expression โŸฉ operator โŸฉ arithmetic โŸฉ bitwise โŸฉ 2's complement

๐Ÿ’ก proof: ~(x ยฑ M) โ‰ก ~x , ~~(x ยฑ M) โ‰ก ~~x (mod M)
// 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

Last updated

Was this helpful?