Yes, if you subtract 229
from 150
, you end up with -79
. However, since you're limited to the range 0..255
(assuming 8 bits) because of the destination, it wraps around to ensure that range is maintained. Hence you basically add 256
to -79
to end up with 177
.
In other words, 9 - 10
which would give you -1
in a signed value, actually gives you 255
for an 8-bit unsigned char
. Similarly, 9 - 11
would give you 254
.
It's no different to addition, with 255 + 7
giving you 6
because it wraps back to 0
following 255
.
Now, the rules are a little more complex than that insofar as the values may be promoted to a larger data type, then the subtraction done, then the result truncated. But mathematically, that first paragraph is correct.