basics · beginner · ~15 min
Reduce a value modulo 256 with correct negative handling.
A process exit status is only a byte. Wrap any integer into 0-255 the way the shell does.
Implement int byte_exit(int n) that returns n reduced into the range 0-255 by wrapping modulo 256, matching how a shell reports an exit status. This must be correct for negative n too (e.g. -1 wraps to 255). No main — the grader calls it.
A single int n (may be negative or larger than 255).
Returns n mapped into 0-255.
byte_exit(0) -> 0
byte_exit(200) -> 200
byte_exit(256) -> 0
byte_exit(257) -> 1
byte_exit(-1) -> 255
% follows the sign of the dividend, so a plain n % 256 is not enough.A single int n (may be negative or > 255).
n wrapped into the range 0-255.
Negative n must wrap to a value in 0-255.
int byte_exit(int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.