Course of Raku / Advanced / More about built-in types / Native types 🆕 / Exercises / Wrapping around
Solution: Wrapping around
Here is a possible solution to the task.
Code
my uint8 $u = 250;
$u += 10;
say $u;
my uint8 $v = 0;
$v--;
say $v;🦋 You can find the source code in the file wrap-around.raku.
Output
4
255Comments
A
uint8ranges from0to255, which is 256 distinct values. Arithmetic on it is effectively done modulo 256.250 + 10would be260, which does not fit. It wraps around, landing260 - 256 = 4past the bottom. So overflow is not limited to stepping over the top by one — any result outside the range is folded back in.Going the other way overflows too: decrementing
0cannot give-1in an unsigned type, so it wraps to the maximum,255.