#include #include #include #include #define MAX_N 1000000 unsigned char b[MAX_N / 4]; int get(int i) { return (b[i/4] >> (2 * (i % 4))) & 3; } void set(int i, int x) { // Clear the previous value if needed b[i/4] &= ~(3 << (2 * (i % 4))); b[i/4] ^= x << (2 * (i % 4)); } int main(void) { srand(time(NULL)); int v[MAX_N]; // For testing // Fill the bitset with junk for (int i = 0; i < MAX_N / 4; i++) { b[i] = rand() % 256; } // Set random values in both arrays for (int i = 0; i < MAX_N; i++) { v[i] = rand() % 4; set(i, v[i]); } // Assert that the arrays contain equal values for (int i = 0; i < MAX_N; i++) { assert(get(i) == v[i]); } }