For a better reading experience, go to: Integer storage in computers – Mculover666’s personal blog.

The data type used to store integers is integer (such as int). What binary code is used to store ** integers in a computer? This article reveals how integers are stored in computers

1. Integer representation in programming (writing)

Integers are expressed in different bases in programming, and there are three ways to express them:

  • Decimal: Direct writing
  • Hexadecimal: prefix 0X or 0X
  • Octal: prefix 0 (zero instead of O)
  • Binary: no, use hexadecimal instead

2. Why not just store the binary code of integers

The decimal integer can be directly converted into binary, see the common base and its conversion article, for the symbol of the integer is represented by the highest bit, the advantage of this is simple and convenient, but this has two disadvantages:

  • Direct storage leads to operation (subtraction) when more complex, is not conducive to the HARDWARE design of CPU, for the addition operation has no impact;
  • The representation of 0 is not unique: since the highest bit is the sign bit, both +0(0 0000000) and -0 (1 0000000) represent 0;

3. Integers are stored in the computer as their complement

3.1. Rules for integer -> complement

The complement of a positive integer

  • The complement of the positive integer: directly convert the positive integer into binary, the left side of the digit is not enough to complement 0.

eg.

On 64-bit machines, if an int in C takes up four bytes, the complement of the positive decimal integer 86 stored in the computer is: 00000000 00000000 00000000 01010110, expressed in hexadecimal as 0x00000056.

Write a program to verify:

# include <stdio.h>

int main(void)
{
	int i = 86;

	printf("i = %d\n", i);
	printf("i = %#x\n", i);

    return 0;
}
Copy the code

The running results are as follows:

The complement of a negative integer

  • The complement of the negative integer: first, the positive integer corresponding to the negative integer is converted to binary, and then all the bits are inverted, and then one is added.

eg.

On 64-bit machines, where an INT in C takes up four bytes, the decimal negative integer -86 is stored in the computer’s complement: 11111111 11111111 111111 10101010, expressed in hexadecimal as 0xFFFFFFAA.

Write a program to verify:

# include <stdio.h>

int main(void)
{
	int i = - 86.;

	printf("i = %d\n", i);
	printf("i = %#X\n", i);

    return 0;
}
Copy the code

The running results are as follows:

Complement of 0

  • The complement of 0 is unique:0

eg.

On 64-bit machines, an int in C takes up four bytes, so the complement of the decimal integer 0 stored in the computer is: 00000000 00000000 00000000 00000000 00000000, expressed in hexadecimal as 0x00000000.

3.2. Rules for complement -> integers

The decimal of the complement of a positive integer

  • Complement of a positive integer: The highest bit 0 indicates the complement of a positive integer, which can be directly converted to decimal.

eg.

On a 64-bit machine, if an INT in C takes up four bytes, the complement code stored in the computer is 00000000 00000000 00000000 01010110, expressed in hexadecimal as 0x00000056, which corresponds to a positive decimal integer of 86.

Write a program to verify:

# include <stdio.h>

int main(void)
{
	int i = 0x56;

	printf("i = %d\n", i);
	printf("i = %#x\n", i);

    return 0;
}
Copy the code

The running results are as follows:

The decimal of the complement of negative integers

  • The complement of a negative integer: The highest bit 1 represents the complement of a negative integer. Invert all the bits and add one to get the absolute value of the corresponding decimal number, followed by a negative sign. eg.

On 64-bit machines, an INT in C takes up four bytes, and the complement stored in the computer is 1111111111 11111111 10101010, expressed in hexadecimal as 0xFFFFFFAA, the corresponding decimal negative integer is -86

Write a program to verify:

# include <stdio.h>

int main(void)
{
	int i = 0xFFFFFFAA;

	printf("i = %d\n", i);
	printf("i = %#X\n", i);

    return 0;
}
Copy the code

The running results are as follows:

The decimal of the complement of 0

  • The complement of 0 is unique, and the corresponding decimal integer is still 0.

4. Data types in C language

In the last section, we learned that integers are stored in the computer as their complement. Now let’s look at a small problem:

4.1. Problem description

Similarly, storing 0x80 in a computer gives a different result:

# include <stdio.h>

int main(void)
{
	char i = 0x80;
	unsigned char j = 0x80;

	printf("i = %d\n", i);
	printf("i = %#X\n", i);

	printf("j = %d\n", j);
	printf("j = %#X\n", j);

    return 0;
}
Copy the code

The running results are shown as follows:

4.2. Analyze the causes

This is because the same 8-bit data 1000 0000 is stored in the computer, but the data type of the variable is different, so the data is interpreted differently:

  • Char is signed by default,1000, 0000,The highest bit of is 1, indicating a negative number, so this complement is interpreted as a negative integer, namely -128;
  • Unsigned char is unsigned, so will1000, 0000,This complement is interpreted as a positive integer, i.e. 128;

5. Integer overflow

After integer overflow, it will count from the beginning, and analyze the two cases of unsigned integer overflow and signed integer overflow:

  • Unsigned integer overflow: The highest number of bits carried. Due to storage space limitations, the excess bits are discarded, so the count is recalculated from 0.
  • Signed integer overflow: the data bit is carried, causing the sign bit to become 1 and the data bit to be cleared, so the count is recalculated from the smallest corresponding negative number.

The sample program is as follows:

/** * @brief Tests the overflow of integers * @author McUlover666 * @date June 26, 2019 14:42:59 * @encoding GBK/GB2312 */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
    int i = INT_MAX;
    unsigned int j = UINT_MAX;

    printf("%d %d %d\n", i, i+1, i+2);
    printf("%u %u %u\n", j, j+1, j+2);

    system("pause");
    return 0;
}
/* result from mingw-w64: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 2147483647-2147483648-2147483647 4294967295 0 1 please press any key to continue... -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
Copy the code