Sign in to follow this  
Sean_Seanston

Bitwise Operator Overloading with Enums?

Recommended Posts

I've searched around but nothing quite seems to deal with this in the way I need it. Let's say I have
enum A
{
 A0 = 0;
 A1 = 1;
};
I want to make it so I can bitwise OR them like
A aVar1 = A0;
A aVar2 = A1;
aVar1 |= aVar2;
Is that possible? If so, how would I do it? Almost all examples seem to deal with classes and adding their variables.

Share this post


Link to post
Share on other sites
You can overload operators for enums in C++, if that's what you want.
Take a look here:
http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/Overloadingoperatorsforenumerations.htm

HTH,
tiv

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
In what language?


Oh crap sorry, it's C++.

Quote:
Original post by tivolo
You can overload operators for enums in C++, if that's what you want.
Take a look here:
http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/Overloadingoperatorsforenumerations.htm


Cool... I think that about shows what I want. Thanks.

Quote:
Original post by samoth
If this is C++, you'll run into trouble because two enums or-ed together are an integer, but not an enum. So... without nasty code (involving reinterpret_cast in overloaded operators) you'll probably not be able to do it.


Yeah... I was kinda wondering about that.

So it's quite complicated to cast them back into enum?

Probably won't have to in my case though I think. Being able to assign to an enum variable the result of OR-ing the integer values of 2 other enums should be enough. I've actually implemented a way of doing this already with a load of brackets and ( int ) etc. but overloading seemed a much neater way of doing it.

Share this post


Link to post
Share on other sites
You could always define a type from scratch. Something like:


class A {
int value;
A(int value): value(value) {}

public:
bool operator==(const A& other) { return value == other.value; }
A operator|(const A& other) { return A(value | other.value); }
A operator&(const A& other) { return A(value & other.value); }
// The "safe bool" idiom doesn't work here because we *do* have a meaningful
// comparison for equality (although maybe we want to ensure operator< etc.
// don't compile). So I've just done the simplest thing.
operator bool() { return value != 0; }

static A foo, bar;
};

A A::foo(1 << 0);
A A::bar(1 << 1);

// ...
bool some_func(const A& flags) {
return flags | A::foo;
}

// ...
some_func(A::foo | A::bar);

Share this post


Link to post
Share on other sites
You can overload operators for enums in C++ too, but I somehow doubt this is what you want.


#include <iostream>

enum Bits { left = 1 << 0, right = 1 << 1, both = left | right };

const Bits& operator|=(Bits& lhv, Bits rhv)
{
lhv = static_cast<Bits>(lhv | rhv);
return lhv;
}

int main()
{
Bits a = left, b = right;
a |= b;
std::cout << a << '\n';
}



This assumes that you give a name to all possible values that the OR operation can produce. If you just want bits to represent flags, you'd make an enum defining names for 1, 2, 4, 8, ..., but then use something like an unsigned to represent combinations of these flags.

Share this post


Link to post
Share on other sites
Quote:
Original post by visitorIf you just want bits to represent flags, you'd make an enum defining names for 1, 2, 4, 8, ..., but then use something like an unsigned to represent combinations of these flags.


Hmm... I hadn't tried that. I assumed OR'ing of the enum just wouldn't work, given the errors I was getting.

Just using an unsigned int seems to have solved the problem perfectly. Thanks all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this