The idea is that if you have $n$ bits $b = [b_{n-1}, \ldots, b_0]$, then you can represent $2^n$ different things with them, as all $b_k \in \{0, 1\}$.
You can decide to go for the numbers $0, \ldots, 2^n - 1$, by saying that let the bit sequence $b$ represent the number $\sum_{k=0}^{n-1} 2^k b_k$.
If you need negative numbers, then you could say that the first bit codes the sign, while the rest specifies the magnitude $0, \ldots, 2^{n-1}$.
It is however more efficient to let the numbers
- $0, \ldots, 2^{n-1} -1 $ be represented as usual: $00\ldots 0$ to $01\ldots 1$,
- $-1, \ldots, -2^{n-1}$ be represented by $1\ldots 1$ to $10\ldots 0$.
Why? Mostly -as far as I know- due to how modular arithmetic works.
We have $n$ bits, so we can perform $\mod 2^n$ addition by simply ignoring the 'final' carry. For example, $1 + 3 = 4$, which is $0$ modulo $4$.
In binary: $01 + 11 = 100$, if we forget about the $1$, we get $00$.
Then it would be really neat if we stored the negative number $-k$ in such a way that
$$k + (-k) = 0$$
held doing our relatively simple $\mod 2^n$ arithmetic, wouldn't it?
So we arrive at the two's complement idea. If you add a number and its bitwise negation, you get a bitsequence of ones. $01+ 10 = 11$. Adding one to that "overflows" and ignoring the carry we get $0$.
So we store $(-k)$ as the bitwise negation of $k$, plus one.
You can encode real numbers (usually with a loss of precision) by saying that the first bit should signify the sign of the number, some bits $(e)$ encode an exponent, and the remaining $(f)$ a fraction. The exponent tells you which powers of two are closest to your number. The fraction part tells you where your number lies between these powers of two.
If you take the number $5$, then
- its sign is $0$ as it is non-negative,
- its exponent is $2$, since it lies between $2^2$ and $2^3$.
- its fraction part codes which bin the number would fall into if we were to divide the interval $[4, 8]$ uniformly into $2^f$ bins. Then the appropriate bit sequence is $010\ldots 0$, since we are in the intervals
- $[4,6]$ (not $[6,8]$)
- (not $[4,5]$) $[5,6]$,
- $[5, 5.5]$, $[5, 5.25]$, etc., until we run out of bits.
For the sake of completeness, the exponent in this case is stored as the "standard" binary representation of the integer $ 2 + 2^{e-1}$. This shift enables the representation of some numbers in the interval $[-1, 1]$, e.g. completely zero exponent bits would correspond to the exponent $-2^{e-1}$.