Re: Finding number of bits of integer
Richard Heathfield <rjh@see.sig.invalid> writes:
> Daniel Kraft said:
>
>> I do need to implement something similar to C++'s std::bitset in C;
<snip>
> The usual way to implement a "bit array", though, is as follows:
<snip>
> 2) allocate (B + CHAR_BIT - 1) / CHAR_BIT bytes (unsigned char
> foo[N] = {0}
<snip>
It is probably worth adding the reason one uses an unsigned integer
type is that shift operations are well-defined on these, and the
reason one uses unsigned char in particular is that it is guaranteed
not to have any padding bits.
--
Ben.
|