在枚举中使用按位或

发布于 2024-12-05 06:07:59 字数 1791 浏览 2 评论 0原文

我需要在控制台应用程序中处理用户的输入,我只需要允许z字段编号(...,-1、0、1,...)。
我通过保留最后一个字符可以验证订单是正确的。最后,用户必须输入z号的集合,例如
-3 6 7 101 -500
我的问题是与lastinput的比较为空间| ...请查看代码。

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
}

我猜我在枚举中做错了什么,因为数字和最后一个是space我无法获得last ==(NextChar.numeric | NextChar.Minus | NextChar.Space)是正确的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-12-12 06:07:59

您确定要进行按位“或”运算吗?

0100 OR 0010 OR 0001 = 0111

这似乎没有帮助。您似乎正在尝试在这里执行布尔逻辑。在这种情况下,您需要将

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

更改为

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

...当然,老实说,如果您受限于一组4 个值,并且您试图确保对其中 3 个值执行某些操作,您可能会更清晰、更具表现力

if(last != NextChar.None)

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)
余生再见 2024-12-12 06:07:59

以下几行说明了如何解释这一点:

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.

您想要的可能是类似:

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

或按位逻辑:

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

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
拥抱没勇气 2024-12-12 06:07:59

用于位标志的一般模式是 ; <操作> (值和掩码))。针对您的具体情况:

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

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))) { ... }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文