4 ms·
The shorts are not promoted to ints (if you even declared the result as that type) until after the multiplication. The result will first be put into a short and
by Veliladon 4y ago
The shorts are not promoted to ints (if you even declared the result as that type) until after the multiplication. The result will first be put into a short and then promoted to int. It's basically asking for an overflow error. Given that 256^2 is 65536 you don't have to be multiplying large numbers before hitting that overflow.
- loeg 4y agoUnsigned short has defined overflow, though.
- shultays 4y agoI don't follow, assuming short is half the size of int it would only overflow if most significant bits of both values are 1. Where did that 256^2 come from? It wouldn't overflow if a and b were 256 I missed that the result would be promoted to signed int, which only most significant bits are set
- deleted 4y ago[deleted]
- LegionMammal978 4y agoAll operands are always promoted to int (or unsigned int, long, unsigned long, etc.) before any operation. The footgun in this example is that even though you're using unsigned integers and would expect guaranteed wrapping semantics, the promotion to signed int makes it UB for USHORT_MAX*USHORT_MAX.