且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何实现在C位集?

更新时间:2022-06-20 01:15:19

CCAN 有一个bitset实现,你可以使用:的http://ccan.ozlabs.org/info/jbitset.html

CCAN has a bitset implementation you can use: http://ccan.ozlabs.org/info/jbitset.html

但是,如果你最终自己实现它(例如,如果你不喜欢在该包的依赖),你应该使用int数组,并使用计算机体系结构的本机尺寸:

But if you do end up implementing it yourself (for instance if you don't like the dependencies on that package), you should use an array of ints and use the native size of the computer architecture:

#define WORD_BITS (8 * sizeof(unsigned int))

unsigned int * bitarray = (int *)calloc(size / 8 + 1, sizeof(unsigned int));

static inline void setIndex(unsigned int * bitarray, size_t idx) {
    bitarray[idx / WORD_BITS] |= (1 << (idx % WORD_BITS));
}

不要使用特定大小(例如,与UINT64 UINT32或),让计算机使用什么希望利用和适应,使用的sizeof。

Don't use a specific size (e.g. with uint64 or uint32), let the computer use what it wants to use and adapt to that using sizeof.