Search code examples
objective-cinstancebitbit-fieldsivar

Instance Variables and Bitfield


I was trying to do an instance field of 1 bit in Objective-C, however when I try this @property BYTE Z : 1; I get an error saying Property name cannot be a bitfield.

What Can't I do so? Is there a workaround this error?

Thanks


Solution

  • The smallest allocable unit of memory is 1 Byte of most machines.There isn't a way to allocate 1 bit, it may be not mappable.It must contain all ASCII characters.
    So just use a Byte and then read the bitmask.

    Use something like:

    @property (nonatomic) Byte byte;
    

    Then use a macro for reading:

    #define BitAtIndex(byte,index) (byte & (1<<index))!=0
    

    PS: Of course the index can't be greater than 7.