basics · beginner · ~15 min

Fits in a byte?

Reason about the range of a fixed-width type.

Challenge

Check whether an integer fits in an unsigned 8-bit value.

Task

Implement int fits_in_uint8(int n) that returns 1 if n is in the range 0..255 (the full range of a uint8_t), else 0. No main — the grader calls it.

Input

A single int n (may be negative or larger than 255).

Output

Returns 1 if 0 <= n <= 255, otherwise 0.

Example

fits_in_uint8(0)     ->   1
fits_in_uint8(255)   ->   1
fits_in_uint8(256)   ->   0
fits_in_uint8(-1)    ->   0

Edge cases

  • Both bounds are inclusive: 0 and 255 fit; -1 and 256 do not.

Input format

A single int n.

Output format

1 if 0 <= n <= 255, otherwise 0.

Starter code

int fits_in_uint8(int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.