fixed point numbers
I've always struggled with binary numbers for some reason, and the concept of fixed point numbers confused me even further. I had to unfortunately get acquainted with them due to my attempts at NDS homebrew.
binary numbers
The binary numbering syste is just a different way of expressing numbers.
Positional numbering systems work by expressing a base that is used to compute the values of each digit in the number.
Foreach digit d
, it's value is:
$$ d \cdot base^{position} $$
And the whole number's value is:
$$ \sum_{i=0}^{L-1}(d_i \cdot b^i) $$
For example 0101 0010
is 82 (2 + 16 + 64)
in decimal.
Digits in binary numbers are called bits. 8 bits make up a byte.
signed binary numbers?
There's a method called two's complement which enables negative binary numbers.
The most significant bit (the one that holds the greatest power of the base) is lost due to encoding the sign information throughout the procedure, so effectively the positive numbers range is halfed to make space for another half of negative numbers.
Two's complement works by inverting all of the bits (greatest number any digit can represent minus the digit to be inverted) and then adding one.
In 4 bits the greatest positive number is 15, which becomes +7 if we interpret the number in two's complement.
The greatest negative number is therefore 1111
which is -8 (15 - 7).
-8 is in reality just 15, but this clever change in intepretation leads to negative numbers and subtractions working with seemingly regular binary numbers.
yes, fixed point now
Fixed point numbers are the product of yet another change in perspective on binary numbers.
Instead of making every bit carry a power of two with a positive exponent, we can leverage a portion of bits with negative exponents to represent fractions! If you recall from the negative power rule: $$ a^{-n} = \frac{1}{a^n} $$
For example, the positional values of a 4.4 (4 integer bits/4 fractional bits) would look like:
2^3 _ 2^2 _ 2^1 | 2^0 | 2^-1 _ 2^-2 _ 2^-3
.
We have an accuracy of 0.125
, our smallest possible increment.
Some random numbers to get the idea across:
0000 1101
=1.625
0011 0000
=3.0
0000 0111
=0.875
Nobody stops us from also using sign bits with fixed point numbers!
That's about it for this tiny post. It probably fails to portray all the shades of sorrow these simple concepts are able to tincture.