Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - In C language, the value range of int is 16 bits. What does 16 bits mean?
In C language, the value range of int is 16 bits. What does 16 bits mean?

1. In the C language, the value range of int is 16 bits. What does 16 bits mean?

Computers use binary to represent values. The smallest unit is bit (bit), which can store 0 or 1, 16 bits means that there are 16 bits that store 0 or 1. The first bit on the left is the sign bit, 0 represents +?, and 1 represents -.

2. What is the relationship between 16 digits and -32768~+32767?

The -32768~+32767 here refers to the range of 16 bits used to store an int value. int is the abbreviation of integer (integer). The value of type int must be an integer, which can be a positive integer, a negative integer or 0.

3. Does 16-bit mean that 32767 has 16 digits when expressed in binary?

No, 16-bit means that 32767 is 0111 1111 1111 1111, and 0000 0000 0000 0000 0111 1111 1111 1111 also means 32767, which has 32 bits.

Extension: About why the value range of 16-bit int value is -32768~+32767.

Convert binary to decimal, count from right to left, the first digit represents 2 raised to the power 0, the second digit represents 2 raised to the power 1, and so on, multiply each digit by the number The corresponding value can be obtained by adding the values ??of the corresponding bits. For example, 1011 is equal to 1*(2^0)+1*(2^1)+0*(2^2)+1*(2^3)=1+2+8=11.

Then the range of 16-bit positive numbers represented by the computer is from 0000?0000?0000?0001 to 0111?1111?1111?1111. (Note: In computer representation, the leftmost is the sign bit.)

That is, positive 1*(2^0) to 1*(2^0)+1*(2^1)+1* (2^2)+……+1*(2^14)=(2^15)-1

That is +1~+32767.

The range of negative numbers requires understanding of the computer's internal method of expressing negative numbers, "2's complement". The specific method is divided into two steps:

In the first step, each binary bit takes the opposite value. , 0 becomes 1, 1 becomes 0.

The second step is to add 1 to the value obtained in the previous step.

For example, to represent 16-bit -1, invert +1, which is 0000?0000?0000?0001, to become 1111?1111?1111?1110, and then +1, which is 1111?1111 ?1111?1111 means -1.

Then to know the absolute value of a negative number represented by the computer, the negative number must be inversely processed according to the "2's complement" rule mentioned above. Therefore, the smallest negative number that can be represented by 16 bits, that is, the sign bit is 1, and the rest are all 0, that is, 1000?0000?0000?0000.

(It is difficult to understand here why the symbol is determined to be 1, and all 0s must be taken later. The main reason is to obtain a value as large as possible after inverse processing, that is, the value at the 2^15th position on the leftmost position. 1, in order to get the smallest negative number)

The first step, 1000?0000?0000?0000 (when processing, this is a binary value, 1 is no longer a symbol)? Subtract 1 to get ?0111?1111?1111?1111.

The second step is to negate it and change it to 1000?0000?0000?0000, which is 1*2^15.

To sum up, the value range of 16-bit int is 1000?0000?0000?0000 to 0111?1111?1111?1111, that is, -2^15~(2^15)-1, -32768~+ 32767.