Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to calculate the range of basic integers in C program: -32768~32767?
How to calculate the range of basic integers in C program: -32768~32767?
Integers in your version are stored in two bytes, each byte is 8 bits, that is, 16 bit binary. As we all know, such a binary number always corresponds to a positive number.

However, how to express positive and negative numbers? Smart scientists use the form of complement to store negative numbers on the premise of limiting the number of binary digits (see complement for algorithm and details).

The first bit is the sign bit, that is, the sign used to represent this number (positive number: 0 negative number: 1), so the largest positive number of int type is 01111. 5438+05 1), its value is 2( 15)- 1, which is the power of 2 minus 1, that is, 32768- 1=32767.

Minimum negative number: 1000000000000. According to the operation of complement, the decimal number corresponding to this binary number is -32768, which also explains why the absolute difference between the largest positive number and the smallest negative number is 1.