Hello all I have a problem with understanding the bitsort program in Bentley's classic programming pearls. I am new to Bitmask and Bitset, so I am not able to understanding these concepts. Actually the program is for "How to sort a disk file?".So below is the code
#include <stdio.h>
#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));
}
void clr(int i) {
a[i>>SHIFT] &= ~(1<<(i & MASK));
}
int test(int i){
return a[i>>SHIFT] & (1<<(i & MASK));
}
int main()
{ int i;
for (i = 0; i < N; i++)
clr(i);
/* Replace above 2 lines with below 3 for word-parallel init
int top = 1 + N/BITSPERWORD;
for (i = 0; i < top; i++)
a[i] = 0;
*/
while (scanf("%d", &i) != EOF)
set(i);
for (i = 0; i < N; i++)
if (test(i))
printf("%d\n", i);
return 0;
}
Could some one please explain this code to me? If possible please provide a version in Java. Actually, I am comfortable in Java only so that why I am asking. This is not homework.