# The mechanics behind exponent bias in floating point

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.

## The mechanics behind exponent bias in floating point

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

**bits:**

**4**- Find the offset using the formula mentioned in IEEE-754 standard:

where **n** is the number of bits. So the offset for

**bits is**

**4****.**

**7**2. Add offset to the original number: ** 3 + 7 = 10**. The resulting number

**is how the number**

**10****is stored under the offset binary scheme. Since we used decimal system to get resulting number**

**3****, we need to convert it to binary:**

**10**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 offsetLink to this section

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

**of them. So, there are only 2 (**

**r****) numbers to choose from —**

**n****and**

**1****, and we want**

**0****(**

**4****) numbers as we’ve got only**

**r****bits:**

**4**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

**in the first case,**

**1****in the second and**

**7****in the third.**

**8**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

**negative numbers**

**8****and exactly the same number of non-negative numbers**

**[-8;-1]****. But the IEEE-754 standard chooses a bit different arrangement with non-negative having two numbers more than negative range —**

**[0;7]****.**

**[-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

**means that we offset by**

**8****numbers down from zero**

**8****and the lowest number we can put into the range is**

**[-8;7]**

**-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

**, which gives us the range**

**7****.**

**[-7; 8]**## Storing numbersLink to this section

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

**bits. We’re going to choose the offset**

**4****calculated by the formula for IEEE-754. Now, if**

**7****is -7, what number do we need to add to get to**

**0000****? It’s**

**3****. Let’s see what this gives us:**

**10**This shows that number ** 3** is stored as

**in binary with offset of**

**1010****. Also, it should be easy to spot where the operation of adding the offset to get the number representation in offset binary comes from:**

**7**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 complementLink to this section

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

**represented with**

**-3****bits. Under offset binary they have the following representation:**

**4**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.

#### Comments (0)

Be the first to leave a comment

About the author

Max is a self-taught software engineer that believes in fundamental knowledge and hardcore learning. He’s the founder of inDepth.dev community and one of the top users on StackOverflow (70k rep).

About the author

##### Max Koretskyi

Max is a self-taught software engineer that believes in fundamental knowledge and hardcore learning. He’s the founder of inDepth.dev community and one of the top users on StackOverflow (70k rep).

About the author

Max is a self-taught software engineer that believes in fundamental knowledge and hardcore learning. He’s the founder of inDepth.dev community and one of the top users on StackOverflow (70k rep).

###### Looking for a JS job?

###### Sr. Developer - Java 8, Spring Boot, React JS, Redux, JavaScript, GraphQL

Firstsource Advantage

Remote