| Shorthand | Meaning |
|---|---|
| uint | Unsigned integer |
| 2C | Two's complement |
| 1C | One's complement |
Binary to decimal conversion can be seen as a dot product between the binary values and a weight vector.
E.g., when , we get the unsigned integer representation.
A similar, slightly more abstract view is to look at it as a linear transform with the weights as the spanning set. This shift in view illuminates the structure of binary representations. Their properties such as their range, injectivity, and behavior under addition can be derived from the corresponding linear functionals.
Under this view, different binary representations -
uint,2C,1Candfixed-point- correspond to different choices of weight vectors.
The top three encodings differ only in their last weight while fixed-point differs in all weights:
| Encoding | Weights |
|---|---|
uint |
|
2C |
|
1C |
|
fixed-point |
An example is shown for 3-bit binary strings. Each column applies a different weight vector to decode the same bits.
Note how the first two transforms - uint and 2C - are injective: each of the 8 input strings maps to a distinct output value. The third one - 1C - is not injective: map to .
This mapping also highlights that i.e. they have the same structure, which is why the same hardware can implement both.
Strictly speaking, the rank of these linear transforms is , and the 1D weight vectors are not linearly independent over real numbers. However, if the only inputs allowed are either or then the weights are effectively independent for uint and 2C.
The weights of 1C have a linear dependence: the last weight is the negative sum of the previous weights, which is why maps to :
Another algebriac structure that helps understand the properties of these representations is the ring. Both uint and 2C representations can be seen as the ring .
Note that each element in the underlying set is , and that in this ring.
Thus from the one can , which gives us the familiar two's complement representation. This makes the isomorphism between the two representation clear: it is the same underlying set and the same addition operation, just with a different choice of representatives for each equivalence class.
Note that:
Thus adding to any of the weights does not change the transform's value in the ring (i.e. mod ), which is why we can have different weight vectors corresponding to the same underlying ring structure.
This can be visualized by first. Now we modify these weights:
Since in the ring, we have:
This equation explains the well known relationship: that to get the two's complement representation of a number, you can take the one's complement and add 1. This can be seen by setting the weights to two's complement and looking at pairs :
Another common encoding scheme is to add a bias (or shift) to the unsigned representation so that the bits represent values from to in that order:
It is used to represent the exponent of a floating point number (in IEEE 754 representation), where it is called the exponent bias.
In this transform, the input does not map to . Thus it is not a linear transform. It is instead an affine transform. Decimal to binary conversion in this case is done by simply adding the bias to the number, then applying the same linear transform as unsigned.