Bitwise Operator Overloading with Enums?

Started by
6 comments, last by Sean_Seanston 14 years, 8 months ago
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.
Advertisement
In what language?
That doesn't look like C++ but in C++ you can do
enum A {
A0 = 1 << 0,
A1 = 1 << 1,
A2 = 1 << 2,
A3 = 1 << 3,
// .. etc
};
Graphics Programmer - Ready At Dawn Studios
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
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.
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.
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);
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.
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.

This topic is closed to new replies.

Advertisement