Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to calculate the range of long integer type
How to calculate the range of long integer type

Understand this way:

Long is generally 4 bytes, that is, 32 bits (numbered 0, 1,...30, 31 respectively).

The highest bit (that is, the bit numbered 31) is the sign bit. If this bit is 1, it means a negative number, and if it is 0, it means a positive number.

The largest positive number is that digits 0 to 30 are all 1, and digit 31 is 0.

For example, 0111 1111 1111 1111 1111 1111 1111 1111

When converted to decimal, it is 2^0 + 2^1 ...+ 2^29 + 2^30 = 2^31 - 1

The negative number -1 expressed in binary is -1111 1111 1111 1111 1111 1111 1111 1111

When converted to decimal it is -2^31 + 2^0 + 2^1...+2^30 = -1

Therefore, the smallest negative binary form is:

1000 0000 0000 0000 0000 0000 0000 0000

That is -2^31 + 0 + 0....= -2^31.