Thread: Bit Flags Issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Bit Flags Issue

    After finishing reading a tutorial on bit flags, i decided to do some practicing.

    Here's what I came up with:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    enum PersonSettings
    {
        Works = 1,
        Studies = 2,
        HasKids = 4,
        LivesAlone = 8
    };
    
    class Person
    {
        public:
            Person(string n, PersonSettings s);
            void printSettings();
        private:
            string name;
            PersonSettings settings;
    };
    
    Person::Person(string n, PersonSettings s) : name(n), settings(s)
    {
    
    }
    
    void Person::printSettings()
    {
        cout<<"WORKS? ";
        if(settings & Works) cout<<"YES"; else cout<<"NO";
        cout<<endl;
    
        cout<<"STUDIES? ";
        if(settings & Studies) cout<<"YES"; else cout<<"NO";
        cout<<endl;
    
        cout<<"HAS KIDS? ";
        if(settings & HasKids) cout<<"YES"; else cout<<"NO";
        cout<<endl;
    
        cout<<"LIVES ALONE? ";
        if(settings & LivesAlone) cout<<"YES"; else cout<<"NO";
        cout<<endl;
    }
    
    int main()
    {
        Person someone("Jack",LivesAlone | HasKids);
        someone.printSettings();
        cout<<endl;
    }
    It works well ONLY if set one bit flag on the constructor, if i try to set more than one bit flag (like in the code above) it displays the following error report:

    error: invalid conversion from `int' to `PersonSettings'|
    line of error:
    Person someone("Jack",LivesAlone | HasKids);
    Oh, and BTW, is this a correct usage of bit flags?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the combined result isn't a valid enum value.

    An enum is a rather more distinct type in C++ than it is in C.
    Incompatibilities Between ISO C and ISO C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    still, it's not clear to me why the OR bit operator doesnt work as it should.

    I'll even write a simpler code that makes the compiler reports the same error message:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int main()
    {
    
          enum mySettings
          {
               opt1 = 1,
               opt2 = 2,
               opt3 = 4,
               opt4 = 8
          };
    
          mySettings options;
          options = opt3 | opt4;
    
          cout<<hex<<options<<endl;
    
          system("PAUSE");
          return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by dhuan View Post
    still, it's not clear to me why the OR bit operator doesnt work as it should.
    As already indicated by Salem, the OR is executed correctly, the result is simply not a valid enum value, so that you cannot assign the result., hence the error. In your example, 4 | 8 = 12. There is no enum member with a value of 12, hence the compiler bails.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Bit Manipulation
    By Sicilian_10 in forum C Programming
    Replies: 4
    Last Post: 03-19-2010, 10:01 AM
  2. How to operate on bit strings > 64 bits
    By Metha in forum C Programming
    Replies: 1
    Last Post: 10-19-2009, 10:55 PM
  3. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  4. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM