guyl
guyl

Reputation: 2242

Using bitwise OR in enums

I need to process the input from the user in a console application, and i need to allow only Z field numbers (... ,-1 , 0 , 1 ,...).
I have built that process char by char from the user, by keeping the last char i can verify that the order is correct. in the end the user must enter set of Z numbers for example
-3 6 7 101 -500.
My problem is with the comparison of the LastInput as enum, meaning i whant to check if the last input was Numeric | Space | ... please take a look at the code.

public void Foo()
{
    ConsoleKeyInfo key;
    var chars = new List<char>();
    NextChar last = NextChar.Space;
    var list = new List<NextChar> {last};

    do
    {
        key = Console.ReadKey(true);
        NextChar next = ProcessCharInput(key.KeyChar);
        switch (next)
        {
            case NextChar.None:
            if(key.Key != ConsoleKey.Enter)
            {
                return;
            }
            continue;

            case NextChar.Space:
            if (last == (NextChar.Numeric))
            {
                Console.Write(key.KeyChar);
                last = next;
                chars.Add(key.KeyChar);
            }

            break;
            case NextChar.Minus:
            if (last == (NextChar.Space))
            {
                Console.Write(key.KeyChar);
                last = next;
                chars.Add(key.KeyChar);
            }
            break;
            case NextChar.Numeric:
            if (last == (NextChar.Numeric | NextChar.Minus | NextChar.Space))
            {
                Console.Write(key.KeyChar);
                last = next;
                chars.Add(key.KeyChar);
            }
            break;
            default:
            throw new ArgumentOutOfRangeException();
        }
    }
    while (true);
}

[Flags]
private enum NextChar
{
    None = 0x0,
    Space = 0x1,
    Minus = 0x2,
    Numeric = 0x4
}

I am guessing that i am doing something wrong with the enum because the input of Numeric and last is Space i cant get the last == (NextChar.Numeric | NextChar.Minus | NextChar.Space) to be true.

Upvotes: 2

Views: 344

Answers (3)

Are you sure you're trying to do a bitwise OR?

0100 OR 0010 OR 0001 = 0111

Which doesn't seem to help. It seems likely that you're trying to do boolean logic here. In which case, you'd want to change

last == (NextChar.Numeric | NextChar.Minus | NextChar.Space)

to

last == NextChar.Numeric || last == NextChar.Minus || last == NextChar.Space

...of course, honestly, if you're constrained to a set of 4 values and you're trying to make sure that you do something on 3 of them, you're probably more clear and expressive with

if(last != NextChar.None)

Upvotes: 4

Mike
Mike

Reputation: 980

The general pattern you want to use for bit flags is <comparison value> <operation> (value & mask)). For your case specifically:

if(NextChar.None != (last & (NextChar.Numeric | NextChar.Minus | NextChar.Space))) { ... }

Upvotes: 2

Anders Abel
Anders Abel

Reputation: 69290

The following lines illustrate how this will be interpreted:

last == (NextChar.Numeric | NextChar.Minus | NextChar.Space)
last == (4 | 2 | 1) // Same meaning with the constants in place.
last == 7 // Same meaning, with the or calculated.

What you want is probably something like:

last == NextChar.Numeric || last == NextChar.Minus || last ==  NextChar.Space

or with bitwise logic:

last & (NextChar.Numeric | NextChar.Minus | NextChar.Space) != 0

Upvotes: 2

Related Questions