如何在 C/Objective C 中实现位数组

发布于 2024-09-24 04:35:58 字数 95 浏览 0 评论 0原文

iOS / Objective-C:我有大量布尔值。

这是存储这些值的一种低效方式——当只需要一个元素时,每个元素至少使用八位。

我该如何优化?

iOS / Objective-C: I have a large array of boolean values.

This is an inefficient way to store these values – at least eight bits are used for each element when only one is needed.

How can I optimise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤城病女 2024-10-01 04:35:58

请参阅 CFMutableBitVector/CFBitVector 了解 CFType 选项

see CFMutableBitVector/CFBitVector for a CFType option

旧人哭 2024-10-01 04:35:58

试试这个:

#define BITOP(a,b,op) \
 ((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a)))))

然后,对于任何不大于 size_t 的无符号整数元素数组,BITOP 宏可以将数组作为位数组进行访问。例如:

unsigned char array[16] = {0};
BITOP(array, 40, |=); /* sets bit 40 */
BITOP(array, 41, ^=); /* toggles bit 41 */
if (BITOP(array, 42, &)) return 0; /* tests bit 42 */
BITOP(array, 43, &=~); /* clears bit 43 */

等等。

Try this:

#define BITOP(a,b,op) \
 ((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a)))))

Then for any array of unsigned integer elements no larger than size_t, the BITOP macro can access the array as a bit array. For example:

unsigned char array[16] = {0};
BITOP(array, 40, |=); /* sets bit 40 */
BITOP(array, 41, ^=); /* toggles bit 41 */
if (BITOP(array, 42, &)) return 0; /* tests bit 42 */
BITOP(array, 43, &=~); /* clears bit 43 */

etc.

-黛色若梦 2024-10-01 04:35:58

您使用按位逻辑运算和位移位。 (Google 搜索这些术语可能会给您一些示例。)

基本上,您声明一个整数类型(包括 intchar 等),然后“移位”整数值到您想要的位,然后与该整数执行“或”或“与”操作。

一些快速说明性示例(C++ 语言):

inline bool bit_is_on(int bit_array, int bit_number)
{
   return ((bit_array) & (1 << bit_number)) ? true : false;
}

inline void set_bit(int &bit_array, int bit_number)
{
   bit_array |= (1 << bit_number);
}

inline void clear_bit(int &bit_array, int bit_number)
{
   bit_array &= ~(1 << bit_number);
}

请注意,这提供了恒定大小(sizeof(int) * 8 位)的“位数组”。也许这对你来说没问题,或者你可能想在此基础上构建一些东西。 (或者重新使用某些库提供的任何内容。)

这将使用比 bool 数组更少的内存...但是...编译器生成的用于访问这些位的代码将更大且更慢。因此,除非您有大量需要包含这些位数组的对象,否则它可能会对速度和内存使用产生净负面影响。

You use the bitwise logical operations and bit-shifting. (A Google search for these terms might give you some examples.)

Basically you declare an integer type (including int, char, etc.), then you "shift" integer values to the bit you want, then you do an OR or an AND with the integer.

Some quick illustrative examples (in C++):

inline bool bit_is_on(int bit_array, int bit_number)
{
   return ((bit_array) & (1 << bit_number)) ? true : false;
}

inline void set_bit(int &bit_array, int bit_number)
{
   bit_array |= (1 << bit_number);
}

inline void clear_bit(int &bit_array, int bit_number)
{
   bit_array &= ~(1 << bit_number);
}

Note that this provides "bit arrays" of constant size (sizeof(int) * 8 bits). Maybe that's OK for you, or maybe you will want to build something on top of this. (Or re-use whatever some library provides.)

This will use less memory than bool arrays... HOWEVER... The code the compiler generates to access these bits will be larger and slower. So unless you have a large number of objects that need to contain these bit arrays, it might have a net-negative impact on both speed and memory usage.

追我者格杀勿论 2024-10-01 04:35:58
#define BITOP(a,b,op) \
   ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))

不起作用...

修复:

#define BITOP(a,b,op) \
 ((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a)))))
#define BITOP(a,b,op) \
   ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))

will not work ...

Fix:

#define BITOP(a,b,op) \
 ((a)[(size_t)(b)/(8*sizeof *(a))] op ((size_t)1<<((size_t)(b)%(8*sizeof *(a)))))
木有鱼丸 2024-10-01 04:35:58

我在编写一个位数组框架时遇到了这个问题,该框架旨在管理类似于 Java BitSet 的大量“位”。我想看看我决定的名称是否与其他 Objective-C 框架冲突。

不管怎样,我才刚刚开始,正在决定是否将其发布到 SourceForge 或其他开源托管网站上。

如果您有兴趣,请告诉我

编辑:我已经在 SourceForge 上创建了名为 BitArray 的项目。源代码位于 SF SVN 存储库中,我还上传了编译后的框架。这个LINK将帮助您到达那里。

  • 坦率

I came across this question as I am writing a bit array framework that is intent to manage large amounts of 'bits' similar to Java BitSet. I was looking to see if the name I decided on was in conflict with other Objective-C frameworks.

Anyway, I'm just starting this and am deciding whether to post it on SourceForge or other open source hosting sites.

Let me know if you are interested

Edit: I've created the project, called BitArray, on SourceForge. The source is in the SF SVN repository and I've also uploaded a compiled framework. This LINK will get your there.

  • Frank
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文