Int multiplication is a negative number problem

Record a problem in which int types are multiplied and eventually become negative numbers.

int a = 60000000;

int b = 36;

When a*b you will find that it is a negative number.

This is because the value range of int (int 32) and 2 raised to the 32nd power is -2,147,483,648~2,147,483,647

If a*b exceeds this range, int will overflow, and the multiplied value will be a negative number.

For larger values, you can use long instead of int

long is Int64, the value range is -9223372036854775808~9223372036854775807

This will generally not exceed

Guess you like

Origin blog.csdn.net/qq_33515628/article/details/106384119