Technology Sharing

The calculation principle of the range represented by int type variables

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Understand

insert image description here
Normally, an int type variable occupies 4 bytes, 1 byte has 8 bits, and each bit has two states: 0 and 1, so an int type variable can represent a total of 2^32 states (that is, it can represent 2^8 data values).

2. Why is the value range of int type integer usually -2147483648 ~ 2147483647?

07:36 Start
Further understanding
        ask:From the above two videos, we can know that the range of positive integers that can be represented by the int type is 1 ~ 2^31 - 1. If we apply this logic to negative integers, shouldn't the range of integers that can be represented by the int type be -2^31 + 1 ~ 2^31 -1? Since the value 2^31 cannot be obtained, why can -2^31 be obtained?
        answer:In computer science, the most common way to represent signed integers isTwo's complement representationThis method not only solves the problem of representing positive and negative numbers, but also simplifies binary addition and subtraction operations. Let's take a closer look at this representation:
        1. A positive number:The binary representation of a positive number is the same as that of an unsigned number. For example, the binary representation of 3 is 011.
        2. negative number: Negative numbers are represented by the complement of positive numbers. The complement of a negative number is calculated by inverting the binary representation of the absolute value of the number and then adding 1. For example:
① Inversion: First, invert the binary representation of 3 (011) to 100.
② Add 1: 100 plus 1 equals 101, so the two’s complement representation of -3 is 101.
        For a 32-bit integer variable, its highest bit (also called the sign bit) indicates the sign, 0 for positive and 1 for negative. The remaining 31 bits are used to represent the value.Now let’s go back to the question we just raised:
        1. Positive range (including 0): The sign bit is 0, and the remaining 31 bits can represent values ​​from 000...000 (31 0s) to 011...111 (31 1s). The corresponding decimal range is from 0 to 2^31 - 1.
        2. Negative range: The sign bit is 1, and the remaining 31 bits can represent values ​​from 100…000 (31 zeros) to 111…111 (31 ones). Among them, the two's complement of 100…000 (31 zeros) represents -2^31, and the corresponding decimal value is -2^31. The two's complement of 111…111 (31 ones) represents -1, and the corresponding decimal value is -1.
        Positive number range: 1 ~ 2^31 - 1; 0; negative number range: -1 ~ -2^31, the total number is still 2^32 different states. Therefore, the value range of int type integers is usually -2147483648 ~ 2147483647 (i.e. -2^31 ~ 2^31 - 1)

3. How many bytes does the int type occupy?

How many bytes does a 64-bit int occupy? The difference between integer types int, long, and long long
How many bytes does a 64-bit int occupy? [Interview] How many bytes does an int occupy?
The difference between 32-bit and 64-bit machines and the number of bytes occupied by basic data types
C language integer type (including value range and length)
How many bytes does the int type occupy?
Data size of int type and pointer type on different platforms
Java sizeof() problem
In Python, int type variables can store integers of any size.

Summarize:

  1. The size of C and C++ data types is affected by compilation standards, compilers, operating system bit numbers, etc.
  2. The data type sizes of Java and Python are determined by the language specification or interpreter implementation and are not affected by factors such as the compiler or the bit number of the operating system.
  3. The main reason for this design difference is that C and C++ are system programming languages ​​that usually need to interact directly with hardware, so the size of data types varies depending on the underlying environment; while Java and Python are high-level programming languages ​​with a higher level of abstraction and a design goal of cross-platform consistency.

4. Recommend

Recommend again
Part III Origin of the Problem