networking · beginner · ~15 min

Count epoll events of interest

Filter the epoll event array by a mask.

Challenge

Count how many epoll event words carry a flag you care about.

Task

Implement int count_matching(const unsigned *events, int n, unsigned mask).

Input

  • events: array of n event bitmasks (as epoll_wait would fill).
  • n: number of entries.
  • mask: the bits of interest.

Output

Return how many entries have at least one bit of mask set (events[i] & mask is non-zero).

Example

events = {1, 2, 1, 4}, mask = 1  ->  2
events = {1, 2, 1, 4}, mask = 8  ->  0

Input format

events: an array of n event bitmasks; mask: the bits of interest.

Output format

The number of entries where (events[i] & mask) is non-zero.

Constraints

An entry matches if it shares any bit with mask.

Starter code

int count_matching(const unsigned *events, int n, unsigned mask) {
    /* TODO */
    return 0;
}

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