basics · beginner · ~15 min
Extract the value of a single bit.
Implement:
int get_bit(unsigned x, int i);
Return bit i (0 or 1) of x. i is in 0..31.
x and a bit index i (0..31).
0 or 1.
i in 0..31.
#include <stddef.h>
/* Return bit i (0 or 1) of x. i in 0..31. */
int get_bit(unsigned x,int i){ (void)x;(void)i; return 0; }
Forgetting to mask with & 1 (returning the whole shifted value).
Bit 0 is the least-significant bit; bit 31 the most-significant.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.