Usually signed integers are stored as two’s complement. However, exponent in IEEE-754 floating point standard is stored as offset binary. This article explains the inner workings of this mechanism.

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:

  1. Find the offset using the formula mentioned in IEEE-754 standard:

where n is the number of bits. So the offset for 4 bits is 7.

2. 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:

If you don’t know the conversion algorithm, or want to know why it works, check out my article on decimal-binary conversion algorithms.

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. We need to use the formula for permutations with repetition:

where n is the number of things to choose from, and we choose r of them. So, there are only 2 (n) numbers to choose from — 1 and 0, and we want 4(r) numbers as we’ve got only 4 bits:

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:

But if we include negative integers, then the range can vary:

What’s interesting here is that although we change the range in decimal, in binary it remains the same, only now the lowest number 0000 represents a negative number. It’s as if this lowest number is offset down from zero by 1 in the first case, 7 in the second and 8 in the third.

I’ve shown you three different ranges, and you might wonder which of them is correct distribution of numbers. As it turns out, there is no single standard that defines how the numbers should be distributed on both sides of zero. The common approach is to have equal quantity of numbers in the negative and non-negative range. So, for 4 bits the range is [-8;7], where there are 8 negative numbers [-8;-1] and exactly the same number of non-negative numbers [0;7]. But the IEEE-754 standard chooses a bit different arrangement with non-negative having two numbers more than negative range — [-7;8].

In order to be able to calculate representation of any number under offset binary system, we need to know how the offset is calculated. Let’s first calculate the offset for common case with negative and non-negative ranges having the same quantity of numbers. It’s easy. If there 2⁴ numbers total, then half of that is 2³ and so the formula for calculating this offset is:

where n is the number of bits available. So having an offset of 8 means that we offset by 8 numbers down from zero [-8;7] and the lowest number we can put into the range is -8.

As I mentioned earlier, IEEE-754 standard decided to have more space for positive numbers and so the offset is one number less for negative numbers range, which gives us the formula for calculating offset as:

With this formula the offset for 4 bits is 7, which gives us the range [-7; 8].

Storing numbers

Now that we know how offset is calculated, let’s see how it can be applied. You may recall from the beginning of the article that when converting a number to binary offset format, it’s added to the original number and when retrieving original number — it’s subtracted. There is simple math that explains that.

For instance, we need to store number 3 in 4 bits. We’re going to choose the offset 7 calculated by the formula for IEEE-754. Now, if 0000 is -7, what number do we need to add to get to 3? It’s 10. Let’s see what this gives us:

This shows that number 3 is stored as 1010 in binary with offset of 7. Also, it should be easy to spot where the operation of adding the offset to get the number representation in offset binary comes from:

Finally, as a consequence, if we added the offset to get number representation in offset binary, we should subtract it to convert number back to the original.

Advantages over two’s complement

The biggest advantage of offset binary over two’s complement is that it allows comparing numbers as is using lexicographical order, without the need of additional operations. For example, let’s compare two numbers 3 and -3 represented with 4 bits. Under offset binary they have the following representation:

Comparing bit by bit, it’s immediately clear for computer from the first bit that the first number is bigger. Lexicographical order can’t be applied to numbers stored as two’s complement:

To compare them computer has to perform additional operations.