Ben Eater's video on two's complement helped me come up with some ideas for this gist.
Interpreting binary numbers is not terribly difficult, but it's also easy to forget how they work. I decided to jot down a few quick things for reference.
Given that each bit can represent either 0 or 1, there are a total of two possibilities per bit. The number of possibilities doubles for each new bit added.
In an 8-bit sequence, each digit, from right to left, is double the previous digit, with the first bit representing 1.
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 = 0
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 1 0 0 = 4
0 0 0 0 1 0 0 0 = 8
0 0 0 1 0 0 0 0 = 16
0 0 1 0 0 0 0 0 = 32
0 1 0 0 0 0 0 0 = 64
1 0 0 0 0 0 0 0 = 128
0 0 0 0 0 1 0 1 = 5
0 0 0 0 1 0 1 0 = 10
0 0 0 0 1 1 1 1 = 15
0 0 0 1 0 1 0 0 = 20
1 1 1 1 1 1 1 1 = 255
Another way to look at it -- given that each bit can represent either 0 or 1, there are a total of two possibilities per bit. The number of possibilites doubles for each new bit added.
- 1 bit = 2 possible numbers (0 - 1)
- 2 bits = 4 possible numbers (0 - 3)
- 3 bits = 8 possilbe numbers (0 - 7)
- 4 bits = 16 possible numbers (0 - 15)
In working with real numbers, computers have to come up with interesting ways to represent certain aspects of a number. Take, for instance, a number's sign (+
/-
). With a byte (8 bits), we're able to represent any positive number between 0 and 255, but what about negative values?
We can use the first bit as a sign bit, with 1
representing -
and 0
representing +
. This changes our number range, but we now can represent negative numbers.
1 0 0 0 0 0 0 1 = -1
0 0 0 0 0 0 0 1 = 1
1 0 0 0 0 0 1 0 = -2
1 0 0 0 0 0 1 1 = -3
1 1 1 1 1 1 1 1 = -127
Like the above example, there have historically been different operations in use to represent negative numbers and transform real numbers in computers, including using operations like one's complement and magnitude notation.
The most popular way for computers to represent signed values, though, is by using a mathematical operation called two's complement.
Quoted from Wikipedia:
The two's complement of an N-bit number is defined as its complement with respect to 2N; the sum of a number and its two's complement is 2N. [...] The two's complement is calculated by inverting the digits and adding one.
We can get the two's complement of a binary number by inverting all the binary values and adding 1. For example:
0001 = 1
1111 = -1
invert -> 1110 -> add 1 -> 1111 = -1
0011 = 3
1101 = -3
invert -> 1100 -> add 1 -> 1101 = -3
0100 = 4
1100 = -4
invert -> 1011 -> add 1 -> 1100 = -4
With two's complement, we consider the biggest bit value to be a negative value, -8 in this case. With that, our binary numbers and their two's complement are opposites of each other.
-8 4 2 1
1 0 1 1 = -8 + 2 + 1 = -5
0 1 0 1 = 4 + 1 = 5
Using the two's complement operation, computers have a way to conveniently represent real numbers with binary values, and a way to easily mutate these real numbers by addition and subtraction.