Can this achieve what you want? Well, I can assume one of four avenues, because you haven't told us exactly what you want (grumble).
- You want to assign the
short
value to auint8_t
, without signed-to-unsigned conversion. In this case, you must be sure that the old value is within the range of auint8_t
:assert(old_value >= 0 && old_value <= 255);
. If this point describes the behaviour you desire and that assertion fails, then no, your code doesn't do what you want. - You want to assign a possibly negative or out-of-range
short
value to auint8_t
and you don't see a problem with the reduction process R described in his comment. If this point describes the behaviour you desire, then yes, your code does what you want. - You want to assign a possibly negative or out-of-range
short
value to auint8_t
and the reduction process R described in his comment doesn't work for you, but you are prepared to express your own reduction algorithm in your code. If this point describes the behaviour you desire, then no, you haven't expressed your custom reduction algorithm. - You want to assign a possibly negative or out-of-range
short
value to auint8_t
and you don't want want the value to be reduced. In this case,uint8_t
is unsuitable. Select a different type.