Learn How Computers Store Integer Numbers

What you will learn:

  • What is an integer
  • How positive and negative integers are stored
  • How to calculate the maximum number available with different size integers

Introduction

This is a very basic overview of how computers store integers. We will guide you through in the most simple way to help you understand this concept in around 10 minutes.

What Is An Integer?

An integer (from the latin word integer meaning whole or complete) is a number without a fraction that can be a negative or a positive whole number. For example -3, 0, 5 and 20 are integers. Numbers with fractions such as 1.2, -10.5 and 20.25 are not integers.

How Do Computers Store Numbers?

You may have heard before that data is stored in computers as 1s and 0s. Let’s explore how these 1s and 0s are used to store data and in particular how integer numbers are stored.

The 1s and 0s in computers are called bits (short for a binary digit). With binary digits we only have 2 values we can represent, a 1 or a 0. 

When we combine multiple bits together we can represent data on a computer.

In this article we are looking at integers so for simplicity we will leave out all the other types of data and just focus on integers.

When we store integers, the position of each bit represents a certain value. When we have a 1 in that position the value gets added to the total and when we have a 0 in that position that value is ignored.

The positions are assigned values from right to left with the value 1, 2, 4, 8, 16, 32, 64, 128 etc with each value being double that of the previous number.

Examples:

Let’s have a quick look at how we store the number 5 using 4 bits:

Bit values: 0 1 0 1

Position Value8421
Bit Value0101

So we have a 1 in the 4 position and a 1 in the 1 position

4 + 1=5

The maximum number we can store if we have all 1 bit values:

Bit values: 1 1 1 1

Position Value8421
Bit Value1111

8 + 4 + 2 + 1=15

To store the number 0 we use all 0s

Bit values: 0 0 0 0

Position Value8421
Bit Value0000

=0

For completeness let’s show an 8 bit integer example and store the number 173:

Bit values: 1 0 1 0 1 1 0 1

Position Value1286432168421
Bit Value10101101

128 + 32 + 8 + 4 + 1=173

So now we know how to store positive numbers, how do we store negative numbers?

Storing Negative Numbers

(Signed Integers Vs Unsigned Integers)

To store negative numbers we use the bit on the very left to represent the sign of the number, this bit is called the sign bit. An integer that uses this sign bit to store a number is called a signed integer.

If we put a 1 in the sign bit (left bit) it will indicate that this number will be negative and if we put a 0 in the sign bit this will indicate that this number will be positive.

So now if we have a signed 4 bit integer this is what each position value will look like:

Position Value-/+421

Let’s have a look at how we store the number -5

Position Value-/+421
Bit Value1101

(negative)  4 + 1=-5

When we have a signed integer we have 1 less bit to store the number so this reduces the maximum numbers we can store.

Calculating Maximum Integer Values

We can calculate the maximum integer value that can be stored with the number of bits available using this maths:

2 ^ (number of bits) – 1

If we have used the left bit for the sign bit we have 1 less bit to use for the number. So a signed 32 bit integer has only 31 bits for the number and 1 bit used to represent the sign.

Some examples:

Calculating the 4 bit unsigned integer maximum number

2^4 – 1=15

On windows calculator:

  • Go to Scientific mode

Calculating the 32 bit signed integer maximum number

Because we have a signed integer the left bit is used to represent whether the number is negative or positive so we can only use 31 bits to represent the number.

2^31 – 1=2,147,483,647

The minimum number that can be stored is -2,147,483,647 (if we put a 1 in the sign bit)

On windows calculator:

  • Go to Scientific mode

Conclusion

So now you should have an idea of how a computer stores integer numbers, negative integer numbers and the maximum and minimum numbers that can be stored given the size of bits used for the integer.

More Resources

https://www.youtube.com/watch?v=USCBCmwMCDA

Disclaimer

All information on this website is for general informational purposes only and is provided in good faith, however, We make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information on the Website.

Under no circumstance shall We have any liability to You for any loss or damage of any kind incurred as a result of the use of the Website or reliance on any information provided on the Website. Your use of the Website and Your reliance on any information on the Website is solely at Your own risk.