← Back to context

Comment by nayuki

3 hours ago

> 32-by-32 signed (you multiply -1 by -1 and get 1, with no overflow)

Wrong. You mentally casted each operand to int16_t before subsequently casting to int32_t. The first step is unjustified.

The correct calculation according to the C standard is: (int32_t)0xFFFF * (int32_t)0xFFFF, which definitely overflows.

> You mentally casted each operand to int16_t before subsequently casting to int32_t. The first step is unjustified.

That's horrifying. Why were unsigned shorts made to convert to signed ints by zero-extension, again?