0

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

hakuna matata
  • 3,243
  • 13
  • 56
  • 93

1 Answers1

0

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.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • I was checking what are available data types in Objective-C, I though why not use BOOL for 1 bit ints, is ti a good idea? – hakuna matata Dec 12 '12 at 17:54
  • 1
    Yes, you could use it.A BOOL commonly used 1 Byte of memory too, but I don't think that you need to save memory so much.You could also avoiding to read the bitmask.That unless you have a lot of BOOL and fear to run out of memory. – Ramy Al Zuhouri Dec 12 '12 at 18:05