AlbaCode
Higher / Computer Systems / Range of Numbers

Range of Numbers

Learn how the number of bits used in a binary number affects the range

Introduction

When using Two's complement we can calculate the range of numbers.

The range of numbers refers to the smallest and largest values that can be stored or represented, based on the number of bits used in a computer system. In simple terms, it's the span from the lowest possible number to the highest possible number you can have with a certain number of binary digits (0s and 1s).

Calculating the Range

When calculating the range we need to know how many bits of binary we are using.

1 bit

If we are only using 1 bit of binary then our place values are as follows:

-1
 

Therefore the only possible values we can make are -1 and 0.

-1
1
-1
0

The range of numbers refers to the span from the lowest possible number to the highest possible number you can make.

Therefore our range for 1 bit of binary is -1 -> 0

2 bits

If we use 2 bits of binary then our place values are as follows:

-21
  

Therefore the possible values we can make are -2, -1, 0 and 1

-21
10
-21
11
-21
00
-21
01

Again, The range of numbers refers to the span from the lowest possible number to the highest possible number you can make.

In this case, the lowest possible number is -2 and the highest possible number is 1.

Therefore our range for 2 bits of binary is -2 -> 1

4 bits

If we use 4 bits of binary then our place values are as follows:

-8421
    

We don't have to list all of the possible values in order to find the lowest and highest.

The lowest value will occur when we have 1 under the negative place value and 0 under all of the positive place values. This gives us -8.

-8421
1000

This highest number will occur when we have a 0 under the negative place value and 1's under all of the positive place values. This gives us 7.

-8421
0111

Therefore our range for 4 bits of binary is -8 -> 7

8 bits

Let's try the same technique for 8 bits.

If we use 4 bits of binary then our place values are as follows:

-1286432168421
        

Adding a 1 under the negative and a 0 everywhere else will give us the lowest value, in this case -128.

-1286432168421
10000000

Adding a 0 under the negative and a 1 everywhere else will give us the highest value, in this case 127.

-1286432168421
01111111

Therefore our range for 8 bits of binary is -128 -> 127

Other numbers of bits

We can continue this method with any other number of bits, but this starts to become unmanageable.

Do we really want to write out all of the place values for 16 bits, 24 bits, 32 bits, or even 64 bits?

Just for reference, the leftmost place value for 64 bits is 9,223,372,036,854,775,808!

Thankfully there is a formula we can use!

The Formula

For larger numbers it is much easier to calulate the range of numbers using a formula.

Let's have a look at what we know so far.

Number of BitsLowest NumberHighest Number
1 Bit-10
2 Bits-21
4 Bit-87
8 Bit-128127
16 Bit-32,76832,767

Can you see a relationship between the lowest number and the highest number for each of the rows?

You might notice that the highest number is the positive version of the lowest number - 1.

In other words, if we know the lowest number then we can remove the negative sign and take away 1 from it. This will always give is the highest number.

But how do we work out the lowest number?

Take a look at our place values again.

-1
1
-21
10
-8421
1000
-1286432168421
10000000

Remember that our place values are the powers of 2, so we could rewrite the tables as this:

-20
1

-20 is -1

-21-20
10

-21 is -2

-23-22-21-20
1000

-23 is -8

-27-26-25-24-23-22-21-20
10000000

-27 is -128

If we compare this to this table again, can you see the correlation?:

Number of BitsLowest NumberHighest Number
1 Bit-10
2 Bits-21
4 Bit-87
8 Bit-128127
16 Bit-65,53665,535

What about if we do this:

Number of BitsLowest NumberHighest Number
1 Bit-200
2 Bits-211
4 Bit-227
8 Bit-27127
16 Bit-65,53665,535

Any guesses as to what -65,536 is?

If you said -215, you would be right!

The lowest number is always -2number of bits - 1

Remember, for the highest number, we remove the negative sign and take away 1! Therefore:

The highest number is always 2number of bits - 1 - 1

If we put all of this together we get the following formula:

-2number of bits - 1 -> 2number of bits - 1 - 1

Using the formula

Now that we know the formula all we have to do is sub in our required number of bits to calculate the range.

-2number of bits - 1 -> 2number of bits - 1 - 1

Example

What is the range of numbers that can be stored using 24-bit binary?

Our number of bits is 24, so let's sub that into the formula

-2number of bits - 1 -> 2number of bits - 1 - 1

-224 - 1 -> 224 - 1 - 1

-223 -> 223

-8,388,608 -> 8,388,607

Therefore our range of numbers for 24 bit binary is -8,388,608 -> 8,388,607