basics · beginner · ~15 min
Use & to test the low bit.
Test whether an integer is even using a bitwise operator.
Implement int is_even(int n) that returns 1 if n is even and 0 if it is odd. Use the bitwise AND operator: the lowest bit (n & 1) is 0 exactly for even numbers. No main — the grader calls it.
A single int n (may be negative).
Returns 1 if n is even, otherwise 0.
is_even(4) -> 1
is_even(7) -> 0
is_even(0) -> 1
is_even(-2) -> 1
& 1) to inspect the low bit.A single int n.
1 if n is even, otherwise 0.
Use the bitwise AND operator.
int is_even(int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.