How do I implement a bit array in C / 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? 回答1: see CFMutableBitVector/CFBitVector for a CFType option 回答2: 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