6 ms·
umm... But how (uint - uint) can be negative? I thought it would warp around 0 into (UINT_MAX - leftovers).
by IYasha 4y ago
umm... But how (uint - uint) can be negative? I thought it would warp around 0 into (UINT_MAX - leftovers).
- loup-vaillant 4y agoYou're right, it cannot, and does wrap around. But some people might think that it could, so putting the cast could increase readability for them. A similar reasoning applies with operator precedence: even though it's very well defined, we tend to forget it, so instead we put additional parentheses. But in a couple specific cases it hurts more than it helps.
- nayuki 4y agoSo, (unsigned int) - (unsigned int) → (unsigned int) is guaranteed on every platform. But relevant to the bad SHA-3 code, the resulting type of size_t - size_t can be surprising. First, size_t is not guaranteed to have any relation with unsigned int; it could be narrower, the same, or wider. For example, size_t could map to unsigned short, unsigned int, unsigned long, or unsigned long long. If size_t is defined as unsigned short (note that size_t must be at least 16 bits), and short is 16 bits wide, and int is 32 bits wide, then the calculation of size_t - size_t will be promoted to (signed int) - (signed int), and thus the result will have the type signed int.