I’d like to learn bit masking. As far as I understand, it is means to store binary values of certain type into one variable.
If the above assumption is true, I figured I could do something like this:
typedef NSUInteger Traits;
enum
{
TraitsCharacterHonest = 0,
TraitsCharacterOptimistic = 1,
TraitsCharacterPolite = 4,
TraitsCharacterDevious = 8,
TraitsPhysicalTall = 16,
TraitsPhysicalBeautiful = 32,
TraitsPhysicalFat = 64,
TraitsPhysicalBigEyes = 128,
TraitsPhysicalRedHair = 256,
};
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) Traits *traits;
@end
Question 1 is, how do I assign more traits to one person?
Question 2 is, do I have to put ever increasing numbers to enum items, or is there a way to indicate this?
Ultimately I want to achieve something like this:
Person *john = [[Person alloc] init];
//here code that assigns john three traits: TraitsCharacterHonest,
//TraitsCharacterOptimistic and TraitsPhysicalBeautiful.
If I understand it correctly, the value of
john.traits should be 100011., reading from right and each place representing that particular enum value / trait..and 0 meaning not having it and 1 meaning having it.
Can you please advice on syntax and explain a particular aspect if needed?
I’d recommend changing a few things:
The enum values can be changed to be a one left-shifted. Makes it a little easier to write, in my opinion.
You don’t need to typedef to NSUInteger, you can declare a enum type directly using
typedef enum.And, as other people have mentioned, your property shouldn’t be a pointer to a Traits type.
My code would look like this:
Setting John’s traits will look like this:
However, while bit-fields are useful to learn, but they’re a real pain to debug. If you want to go and print
this character’s traits now, you’ll have to write code like this:
Additionally, syntax for operations like removing a trait are confusing. You’ll have to use
&and a NOT-ed constant,If you can (and performance isn’t too much of a issue), I’d prefer using a higher-level feature. Perhaps an NSSet with string constants? e.g.
Then you can do:
Makes more sense to me. What’s more, you can print the description of the traits directly with
and you’ll get reasonable output.