Why do we need biased exponent ?
Introduction:
The following algorithm (for an n-bit two’s complement architecture) sets the result register R to −1 if A < B, to +1 if A > B, and to 0 if A and B are equal:
// reversed comparison of the sign bit
if A(n-1) == 0 and B(n-1) == 1 then
return +1
else if A(n-1) == 1 and B(n-1) == 0 then
return -1
end
// comparison of remaining bits
for i = n-2...0 do
if A(i) == 0 and B(i) == 1 then
return -1
else if A(i) == 1 and B(i) == 0 then
return +1
end
end
return 0
However, Unsigned binary numbers can be ordered by a simple lexicographic ordering, where the bit value 0 is defined as less than the bit value 1. So the best way is to transform the data to unsigned type by adopting offset.
Usually signed integers are stored as two’s complement. However, exponent in IEEE-754 floating point standard is stored as offset binary. It also has many other names, like biased exponent or offset-k, where k denotes an offset. If you google around on how to represent a number using this scheme, you’ll find that you just need to calculate an offset (bias), and then add it to the number you’re trying to store. The resulting number is what actually gets stored. Then convert to binary, if necessary.
To demonstrate the steps let’s see how the number 3 can be stored in 4 bits:
- Find the offset using the formula mentioned in IEEE-754 standard:
K = 2 n − 1 − 1 K=2^{n-1}-1 K=2n−1−1
where n is the number of bits. So the offset for 4 bits is 7.
- Add offset to the original number: 3 + 7 = 10. The resulting number 10 is how the number 3 is stored under the offset binary scheme. Since we used decimal system to get resulting number 10, we need to convert it to binary:
1 0 10 = 101 0 2 10_{10}=1010_2 1010=10102
Defining offset
Suppose we have only 4 bits to store numbers. How many different numbers can we represent with 4 bits? It’s easy to calculate:
2 4 = 16 2^4=16 24=16
The calculation shows us that we can store 16 different numbers in 4 bits. The question is what those numbers might be. Suppose we’re interested in storing only non-negative integers. Then, the range is: