Reputation: 1925
I have to store what values from the range 0-127 appear somewhere. So, I think to store it in type that it's size is 128 bits, so that if the bit is "turn on" the number appear, and if not the number is missing. eg. if the first, sixth and nineties is 1, then the numbers 1,6,90 are appear.
How can I define by typedef type in c that contain 128 bits exactly?
Upvotes: 0
Views: 502
Reputation: 539685
(The following solution is restricted to BSD systems like Mac OS X.)
You could use the bit-string manipulation macros from <bitstring.h>
.
Example:
int numBits = 128;
bitstr_t mybits[bitstr_size(numBits)];
// Alternatively: dynamic allocation:
// bitstr_t *mybits = bit_alloc(numBits);
// Set bits:
bit_nclear(mybits, 0, numBits - 1);
bit_set(mybits, 1);
bit_set(mybits, 6);
bit_set(mybits, 90);
// Test bits:
for (int i = 0; i < numBits; i++) {
if (bit_test(mybits, i)) {
printf("%d ", i);
}
}
Upvotes: 2