I started reading "Programming Pearls" today and while doing it's exercise I came across this question "How would you implement your own bit vector?". When I looked at the solution it was like this:
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[1 + N/BITSPERWORD];
void set(int i) { a[i >> SHIFT] |= (1 << (i & MASK));
Where I am getting confused at is this statement
1 << (i & MASK)
Could someone please explain me what's going on here?
(i & MASK)
with(i % 32)
? If it will be valid but not elegant then could you please shed some light on whyi & MASK
is preferred overi % 32
? Thanks a lot. – Hilarity