Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:21+00:00 2026-06-11T02:55:21+00:00

I’d like to learn bit masking. As far as I understand, it is means

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:55:22+00:00Added an answer on June 11, 2026 at 2:55 am

    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:

    typedef enum
    {
        TraitsCharacterHonest       = 1 << 0,
        TraitsCharacterOptimistic   = 1 << 1,
        TraitsCharacterPolite       = 1 << 2,
        TraitsCharacterDevious      = 1 << 3,
        TraitsPhysicalTall          = 1 << 4,
        TraitsPhysicalBeautiful     = 1 << 5,
        TraitsPhysicalFat           = 1 << 6,
        TraitsPhysicalBigEyes       = 1 << 7,
        TraitsPhysicalRedHair       = 1 << 8
    } Traits;
    
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    @property (strong, nonatomic) NSString  *name;
    @property (assign, nonatomic) Traits     traits;
    
    @end
    

    Setting John’s traits will look like this:

    Person *john = [[Person alloc] init];
    
    john.traits = TraitsCharacterHonest | TraitsCharacterOptimistic | TraitsPhysicalBeautiful;
    

    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:

    NSMutableString *result = [NSMutableString string];
    
    if (self.traits & TraitsCharacterHonest)
    {
        [result appendString: @"Honest, "];
    }
    if (self.traits & TraitsCharacterOptimistic)
    {
        [result appendString: @"Optimistic, "];
    }
    if (self.traits & TraitsCharacterPolite)
    {
        [result appendString: @"Polite, "];
    }
    // etc...
    

    Additionally, syntax for operations like removing a trait are confusing. You’ll have to use & and a NOT-ed constant,

    // remove 'Tall' trait
    john.traits = john.traits & ~TraitsPhysicalTall
    

    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.

    __unused static NSString *TraitsCharacterHonest = @"TraitsCharacterHonest";
    __unused static NSString *TraitsCharacterOptimistic = @"TraitsCharacterOptimistic";
    __unused static NSString *TraitsCharacterPolite = @"TraitsCharacterPolite";
    // etc...
    
    @interface Person : NSObject
    
    @property (strong, nonatomic) NSString     *name;
    @property (assign, nonatomic) NSMutableSet *traits;
    
    @end
    

    Then you can do:

    // adding
    [john.traits addObject: TraitsCharacterHonest];
    // checking
    [john.traits containsObject: TraitsCharacterHonest];
    // removing 
    [john.traits removeObject: TraitsCharacterHonest];
    

    Makes more sense to me. What’s more, you can print the description of the traits directly with

    NSLog(@"John's traits: %@", john.traits);
    

    and you’ll get reasonable output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.