Solution (source code)

= Solution

An $n$-<bit> <two's complement> word $b_{n-1}\cdots b_1b_0$ represents the <integer>
$$
\boxed{x=-b_{n-1}2^{n-1}+\sum_{i=0}^{n-2}b_i2^i,\qquad
-2^{n-1}\leq x\leq2^{n-1}-1.}
$$
The leading <bit> has negative weight. Words beginning with zero encode the nonnegative values; words beginning with one encode the negative values. Equivalently, interpret the whole word as unsigned and subtract $2^n$ when its leading <bit> is one. For example, the four-bit words `0111`, `1000`, and `1111` encode $7$, $-8$, and $-1$ respectively. The range is asymmetric: the most negative value has no positive counterpart in the same number of <bits>.

For a nonzero representable magnitude, complementing all <bits> and adding one obtains its negative modulo $2^n$. Fixed-width addition can therefore use the same low-order <binary numeral system> addition circuitry for signed and unsigned operands; interpreting the result as signed requires detecting <integer overflow>.