3 ms·
If short = 16 bits and int = 16 bits, then x and y will be promoted to unsigned int. Unsigned multiplication has wraparound behavior, so x*y will be defined for
by nayuki 4y ago
If short = 16 bits and int = 16 bits, then x and y will be promoted to unsigned int. Unsigned multiplication has wraparound behavior, so x*y will be defined for all values.
If short = 16 bits and int = 32 bits (or heck even 17 bits), then x and y will be promoted to signed int. Signed multiplication overflow is undefined behavior, so x*y will be undefined for some values when x*y is too large. In particular, 0xFFFF * 0xFFFF = 0xFFFE_0001, which is larger than INT_MAX = 0x7FFF_FFFF.
If short = 16 bits and int = 64 bits (or even 33 bits), then x and y will be promoted to signed int. The range of x*y will always fit int, so no overflow occurs, and the expression is defined for all input values.
Isn't C fun?