|
|
|
|
|
by kazinator
988 days ago
|
|
> however I'm not sure what the utility in that restriction is. We have moved the goalposts much farther apart. If we are using a 32 bit integer type, all we need is that a, b and c fit into 31 bits. Then there is no way that b + c or a - b overflow. For a single addition or subtraction, we just need one bit of headroom. I.e. the values do not actually have to be that small. There are all kinds of situations in which programs work with small integers, where the calculations could bork if an unsigned creeps in. A cliff near zero is qualitatively different from clipping at two extremes. An electronic device that clips everything below zero volts will distort even the faintest waveform. One that clips near the power rails has clean headroom. |
|
If b = 0x40000000 and c = 0x40000000, b and c both fit in 31 bits, and b + c overflows to -2147483648 in signed int32 twos-complement math (I think).
Maybe the definition of "32 bit integer type" you're using is meant to encompass only 32 bits as all unsigned (but then there are a - b terms that would overflow if b > a).
Or perhaps I've gotten something else wrong.