Misbehaving int and long variables

For the UNO it is stated that range limits of int variables, signed and unsigned, are: -32,768 to 32,767 and 0 to 65,535 respectively. And range limits for long variables, signed and unsigned, are: 2,147,483,648 to 2,147,483,647 and 0 to 4,294,967,295 respectively.

I want to calculate the product of two numbers whose range fits into the long category but fail to get the correct results. The following output from serial monitor, giving results for int and long variables, with unexpected output colored red. illustrates:

Results for variable = 33000
-32536 : integer
33000 : unsigned integer
33000 : long
33000 : unsigned long

Results for 33 * 1000 =
-32536 : integer
33000 : unsigned integer
-32536 : long
4294934760 : unsigned long

Results for variable = 66000
464 : integer
464 : unsigned integer
66000 : long
66000 : unsigned long

Results for 66 * 1000 =
464 : integer
464 : unsigned integer
464 : long
464 : unsigned long

What is going on?

variable_issues.ino (1.82 KB)

33 * 1000

Integer multiplied by integer gives an integer result. Then the result is converted to long.
You have to force the multiplication to be done as long integer to get the required result.
33 * 1000L or (long)33 * 1000 etc.

Pete

Integer multiplied by integer gives an integer result.

"int" multiplied by "int" gives an "int" result.

picky picky.
But correct.
Ta

Pete