In limits.h's documentation, it states:
A common reason that a program needs to know how many bits are in an integer type is for using an array of unsigned long int as a bit vector. You can access the bit at index n with:
vector[n / ULONG_WIDTH] & (1UL << (n % ULONG_WIDTH))
But if vector is an unsigned long we'd need n <= ULONG_WIDTH, meaning that the division there would always result in 0. Is vector supposed to be defined instead as an array of unsigned longs or something else?
CodePudding user response:
vector is an unsigned long we'd need n <= ULONG_WIDTH
Vector is an array of unsigned longs.
n is not limited by ULONG_WIDTH. n is the bit position within all the bits in all elements in the array. If you would lay out all bits in all vector elements one after another, n is the bit position within all the bits.
|----------vector[0]----------->|----------vector[1]----------->|----------vector[2]----------->
abcde......................................................................x....................
^LSB MSB^ etc.
The bit marked x has position n = 2 * 32 12 = 76, if i'm counting right. To access it, you have to access 12'th bit of 2'nd element of vector. Note: I am indexing from 0, first bit has position 0, first vector element is element number 0.
n / ULONG_WIDTH computes the 2'nd vector element.
n % ULONG_WIDTH computes the 12'th bit position inside 2'nd vector element.
(1UL << (n % ULONG_WIDTH)) is the mask 0b00.....1 with 12'th bit set.
vector[n / ULONG_WIDTH] & (1UL << (n % ULONG_WIDTH)) returns the 12th bit set if 12th bit is set in vector[2], or returns 0.
Typically in the same fashion:
vector[n / ULONG_WIDTH] |= (1UL << (n % ULONG_WIDTH)) set's the bit,
vector[n / ULONG_WIDTH] &= ~(1UL << (n % ULONG_WIDTH)) clears the bit,
vector[n / ULONG_WIDTH] ^= (1UL << (n % ULONG_WIDTH)) toggles the bit.
