MeTitus
MeTitus

Reputation: 3428

Bitwise operation on non flag Enum

Given the following enum:

enum MyEnum
{
    ValueOne = 1,
    ValueEmpty,
    ValueTwo = 2,
    ValueThree = 2,
    ValueFour = ValueOne | ValueEmpty,
    ValueFive = ValueTwo | ValueThree
}

What are the involved operations in the ValueFour and ValueFive elements, because these are the values I get?

//Is assigned 3
var valueOne = (int) MyEnum.ValueFour;

//Is assigned 2
var valueTwo = (int)MyEnum.ValueFive;

Thanks

Upvotes: 0

Views: 101

Answers (1)

I4V
I4V

Reputation: 35353

ValueEmpty is 2 (ValueOne +1)

valueFour is 3 (2 | 1) (Bitwise or, 0010 OR 0001= 0011)

ValueFive is (2 | 2) which is 2 (Bitwise or, 0010 OR 0010 = 0010)

Upvotes: 5

Related Questions