Convenient ways to work with large bit mask

↘锁芯ラ 提交于 2020-01-14 18:43:47

问题


I am trying to implement an algorithm within an iOS app that will make use of large bitmasks. Each bitmask can be up to 256 bits (32 bytes) in length. I will need to quickly get/set bits at arbitrary locations within the mask, etc.

Are there any built-in language features of C or Objective-C that makes this kind of thing easy to do, or should I expect to write my own functions for manipulating bits within arbitrary character arrays? It doesn't seem like it would be too hard to do it myself but if there's something already available that does this efficiently and cleanly, I'd rather not write my own.


回答1:


Take a look at CFMutableBitVector, which is designed to handle this kind of problem.




回答2:


You might look at std::bitset from the C++ STL.




回答3:


Well, 32 bytes isn't especially large.

You can get/set bits by creating an array of 8 mask bytes, indexing the array with the low 3 bits of the bit #, and using the remaining 5 bits of the bit # to address the array of bytes holding the bits.

I don't recall that there are and/or/xor byte string operations in the standard C library, but it's not to hard to write, eg, a function to do andbytes(target, source, count), in order to do operations between sets of your bits.

The main thing you need to do is to figure out how you will control the storage for your bit arrays. If they're fixed at 256 bits it would make the most sense to make them Objective-C objects with a char[8] array containing the bits.




回答4:


The LibTomMath library has the logical operations necessary on integers of arbitrary size.

Apple's iOS Accelerate Framework presumably provides logical operations on 256 bit integers (as well as other sizes up to 1024 bits), but the documentation is sketchy.



来源:https://stackoverflow.com/questions/7395998/convenient-ways-to-work-with-large-bit-mask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!