What is the correct way to clamp unsigned ints?
For example, say I have:
unsigned int ui = 5U;
Now I want to subtract a value from it:
ui = Clamp(ui - MAGIC_VALUE, 0, 255);
I would like ui to contain 0 if MAGIC_VALUE >= 5.
However, I can't just subtract MAGIC_VALUE as the expression ui - MAGIC_VALUE
will wrap around to UINT_MAX
if MAGIC_VALUE > 5 and end up being clamped to the upper limit, 255. That's the opposite of what I want!
Is there any trick to deal with this?