Last active
January 7, 2022 12:22
-
-
Save w-vi/10093608 to your computer and use it in GitHub Desktop.
bit vector in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Bit set(vector) macros | |
*/ | |
#ifndef BITSET_H_ | |
#define BITSET_H_ | |
/* | |
Taken from comp.lang.c FAQ list · Question 20.8 | |
Don't forget to include <limits.h> for CHAR_BIT. | |
Example: | |
#include <stdio.h> | |
#include <string.h> | |
#define MAX 10000 | |
int main() | |
{ | |
char bitarray[BITNSLOTS(MAX)]; | |
int i, j; | |
memset(bitarray, 0, BITNSLOTS(MAX)); | |
for(i = 2; i < MAX; i++) | |
{ | |
if(!BITTEST(bitarray, i)) | |
{ | |
printf("%d\n", i); | |
for(j = i + i; j < MAX; j += i) | |
BITSET(bitarray, j); | |
} | |
} | |
return 0; | |
} | |
*/ | |
#include "common.h" | |
BEGIN_C_DECLS | |
#define BITMASK(b) (1 << ((b) % CHAR_BIT)) | |
#define BITSLOT(b) ((b) / CHAR_BIT) | |
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b)) | |
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) | |
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b)) | |
#define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT) | |
END_C_DECLS | |
#endif /*_BITSET_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment