# Two's complement general formula

In college, we've seen some general formulas to compute the decimal value a of a binary representation $$a_{N-1} \dots a_2 a_1 a_0$$ in two's complement, $$N$$ being the number of bits. For $$a \ge 0$$ we've seen $$a= \sum_{i=0}^{N-2} a_i 2^i$$, which seems correct to me if I work it out. But for $$a < 0$$ our book says $$a=-2^{N-1}+\sum_{i=0}^{N-2} a_i 2^i$$ and this is where I go wrong.

Can someone explain with an example why this is correct or which formula is right?

Let $$a_{N-1} \cdots a_0$$ be the two's complement representation of the number $$a$$. The more general formula is: $$a = -a_{N-1}2^{N-1} + \sum_{i=0}^{N-2} a_i 2^i$$ (I am sure you can figure out how your formulas can be derived from this one.)
Why does this hold? For $$a \ge 0$$, $$a_{N - 1} = 0$$ and then the value of $$a$$ is simply the value of the binary representation $$a_{N-2} \cdots a_0$$. For $$a < 0$$, on the other hand, let $$b = -a \ge 0$$ have binary representation $$b_{N-1} \cdots b_0$$. Then $$\sum_{i=0}^{N-1} a_i 2^i = \sum_{i=0}^{N-1} (1-b_i) 2^i + 1 = \sum_{i=0}^{N-1} 2^i - \sum_{i=0}^{N-1} b_i 2^i + 1 = 2^N - b$$ since the two's complement is defined as the complement of the representation $$b_{N-1} \cdots b_0$$, plus one, and $$\sum_{i=0}^{N-1}2^i = 2^N - 1$$. Since $$a$$ is negative, $$a_{N-1} = 1$$ and then: $$a = -b = -2^N + \sum_{i=0}^{N-1} a_i 2^i = -2^N + 2^{N-1} + \sum_{i=0}^{N-2} a_i 2^i = -2^{N-1} + \sum_{i=0}^{N-2} a_i 2^i$$