Strange math

The following sketch prints: -1 while the result of this operations should be 127. Does anybody know why?

void setup()
{
  Serial.begin(38400);
  Serial.println(1024 * 127 / 1024);
}

void loop()
{
}

Because, by default, the Arduino works in 16 bit integers.

1024 * 127 is 130048. Truncated to 16 bits is 64512. Represented as a signed integer that's -1024.

-1024 / 1024 = -1.

Add an L to the end of your numbers to force it to use long maths:

  Serial.println(1024L * 127L / 1024L);

Thank you very much!