Menu Close

How are Integers Stored in Memory ?

Posted in C Programming, Computer Science

Integers are typically stored in memory using a fixed number of bits, depending on the size of the integer. For example, a 32-bit integer can store values from -231 to 231-1.

The binary representation of the integer is stored in memory using these bits, with the most significant bit representing the sign of the integer (0 for positive, 1 for negative) and the remaining bits representing the magnitude of the integer.

The process of storing integers in memory is handled by the computer’s central processing unit (CPU), which can perform arithmetic and logical operations on the bits representing the integers. The CPU also includes registers, which are small, fast storage locations used to hold frequently accessed data, including integer values.

When a program accesses an integer value, the CPU retrieves the bits representing the integer from memory and stores them in one of its registers, where it can perform calculations on the integer. Once the calculation is complete, the CPU stores the result back in memory as a series of bits, which can be read and interpreted by the program.

In the C language, the value of an integer variable can be expressed in decimal, octal, or hexadecimal format, but it is stored in memory as a binary number.

1.The Storage Method of Positive Integers in Computers

For example, to convert decimal 65 to binary, first convert it into binary number as 1000001. For specific methods of converting decimal to binary, please refer to this link.

When we declare a integer variable

int i;
i=65;

The memory will arrange a 32 bit memory to store the positive integer in following method:

Related:   Memory and Variables in C Programming

The sign bit  is also called the MSB (most significant bit).  If the sign bit is 0, it is a positive integer. If a sign bit is 1, it is a negative integer.

2.The Storage Method of Negative Integers in Computers

Computer uses special mechanism to store negative numbers which is 2’s complement format.

Before going to that, let’s have a look on 1’s complement of a number.

1’s complement of a number

1’s compliment of number is just inverting binary bits of an actual number.

2’s complement of a number

To get 2’s complement of a number, just add 1 to 1’s complement of an actual number.

This 2’s complement form will be stored in computer. 

Both one’s complement and two’s complement can not change the sign bit (MSB)

When we declare a negative integer variable,

int i;
i=-10;

The memory will assign above memory for -10 ( 16 bit system )

Leave a Reply