networking · beginner · ~15 min

Is it a loopback address?

Recognise the loopback range.

Challenge

Tell whether a dotted-quad IPv4 address is a loopback address (in 127.0.0.0/8).

Task

Implement int is_loopback(int a, int b, int c, int d) where a.b.c.d is the address.

Input

  • a, b, c, d: the four octets of an IPv4 address.

Output

Return 1 if the address is in 127.0.0.0/8 (first octet is 127), else 0.

Example

is_loopback(127, 0, 0, 1)  ->  1
is_loopback(127, 5, 5, 5)  ->  1   (any 127.x.x.x is loopback)
is_loopback(8, 8, 8, 8)    ->  0

Input format

a, b, c, d: the four octets of an IPv4 address.

Output format

1 if the first octet is 127 (127.0.0.0/8), else 0.

Constraints

The entire 127.0.0.0/8 block is loopback; only the first octet matters.

Starter code

int is_loopback(int a, int b, int c, int d) {
    /* TODO */
    return 0;
}

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