Today, I finally began to read the C ++ primer, which is called the C ++ holy book. It is quite detailed. Because I didn't have a solid foundation for C ++ in college, c ++ was used for development when I first came out of work, so I felt quite bitter that others had to take a step ahead of themselves and had to make up for it by squeezing time.
Let's get down to the truth. Enter the question directly.
Built-in types of C ++Int, Char, float, etc.It is closely related to the representation in computer memory. The computer stores data in bit sequence. Each bit stores 0 or 1. The memory has no structure or meaning at this level. Currently, most computers use blocks of a specific number of digits to process storage. The number of digits of a block is usually the power of 2. Therefore, 8-bit, 16-bit, or 32-bit data can be processed at a time.
Here, we have to talk about the unsigned type and the signed type. The unsigned type indicates positive or 0, while the signed type can be positive or negative, as the name suggests, the highest bit of a variable in the binary representation of a variable is 0, indicating that the variable is a positive number. If it is 1, the variable is a negative number, and other bits other than the highest bit indicate the real value, for example, if "0111 1111" is of the symbolic type, it indicates that the value is 127, and the highest digit 0 indicates that the value is a positive number. The signed "0111 1111" indicates the negative 127, and the highest bit 1 indicates that the variable is a negative number. Each bit of the unsigned type is used to represent the value of the variable. For example, the unsigned "1111 1111" is represented as 255, because the compiler uses the highest value as the bit value.
In fact, whether it is a signed type or a non-Signed type, they are stored in the same computer. For example, for signed 1111 1111 and unsigned 1111, they are both stored in binary mode on computers, the difference is whether to calculate the highest bit into the bit value and use it as the basis for positive and negative judgment.
However, in a computer, negative numbers are expressed in the form of a positive complement. The complement code is a positive value of the reverse code plus 1. For example, int A =-2; then expression A on the computer is as follows:
The binary values of positive values of a are as follows: 0000 0000 0000 0000 0000 0000 0000 0010;
The inverse code of the positive value of A is as follows: 1111 1111 1111 1111 1111 1111 1111 1101;
A's complement Code 1111 1111 1111 1111 1111 1111 1111 1101 1111 + 1 = 1111 1111 1111 1111 1111 1111 1110;
The source code of positive numbers is the same as the complement code.
A simple test was conducted:
Unsigned short a = 32769;
Short B =;
Unsigned short (2 16 ) The range is from 0 ~ 65535, while short (2 15 ) Is from-32767 ~ 32767. In this case, when the value of A is assigned to B, it is obviously beyond the range of B. What will the compiler do? As mentioned above, whether it is a signed or unsigned type, the storage mode of variables on the computer will not change. Well, let's take a look at the storage of A in the computer. Use Microsoft's calculator to calculate it as 1000, 0000, 0000, 0001, and so on, when Microsoft's calculator is converted from a computer code to a decimal number, it seems to be converted to an unsigned type, that is, a positive number. However, Microsoft's calculator can represent a negative number in binary format, the representation is also in the form of a complement. When the storage of A is 1000 0000 0000 0001, it can be seen from the highest bit 1 that when a is assigned a signed variable, it will definitely be a negative number, then, the real binary value is introduced through the reverse process of the complement code. First, 1000 0000 0000 1000-1-1 is 0000 0000 0000 0111, and the anti-Code 1111 1111 1111 32767 is obtained. Using Microsoft's calculator, we found that the value is, therefore, if A is of the signed type, the value of a is-32767, and the value of B is-32767 during debugging. OK is verified.
Now let's assume that there is such a situation:
Short A =-2;
Unsigned int B =;
Let's take a look at how a is stored on a computer, 1111 1111 1111 1110, but the integer type B is allocated at least 32 bits in the memory. When a is assigned to B, the value of one bit is assigned from the low position, the remaining bits are assigned values based on the value of the highest bits of A. Therefore, B is 1111 1111 1111 1111 1111 1111 1111 1110. The small bits are converted to the large bits, here is a summary: For the unsigned data, the low byte remains unchanged, and the high byte is supplemented with 0; For the signed data, if the symbol bit is 0, it is the same as the conversion rule for the unsigned data. If the symbol bit is 1, the low byte remains unchanged, and the high byte is set to 1. Therefore, when a is converted to an unsigned integer, short is signed and negative, so all bits with more than 8 bits are 1. . However, if the value assignment process is out of bounds, for example, a demo in C ++ primer, The 8-bit unsigned char variable C ranges from 0 ~ 255, but if we assign 336 to C now, it is obviously out of bounds. This is to save the value after the modulo of 336 for the value of this type range to C, for example, the value stored in this demo is 336% 256 = 80, so C is 80, but the specific machine code is how a value assignment conversion process will be further supplemented.
Next, we will add another image, which is an image of someone else. The Compiler automatically performs implicit conversion between different types of computation: