关于Enum操作
我有一个像
enum Test
{
A = -2,
B = -1 ,
C = 0 ,
D = 1 ,
E = 2
}
and 这样的枚举,我如何判断一个枚举值是在
class Program
{
static void Main(string[] args)
{
Test t1 = Test.A | Test.E;
Console.WriteLine((t1 & Test.E) > 0); // true
Console.WriteLine((t1 & Test.A) > 0); // why this is false ?
Console.ReadKey();
}
}
我想问的组合枚举值中,为什么
Test t1 = Test.A | Test.E;
但是
Console.WriteLine((t1 & Test.A) > 0);
谢谢......
更新:
感谢您的评论,以及好的设计...
*我想我会像儿子一样尽快改变糟糕的设计! *
*仍然谢谢你们。 (^^メ)*
I have a enum like
enum Test
{
A = -2,
B = -1 ,
C = 0 ,
D = 1 ,
E = 2
}
and , how can i judge a enum value is in a combine enum values
class Program
{
static void Main(string[] args)
{
Test t1 = Test.A | Test.E;
Console.WriteLine((t1 & Test.E) > 0); // true
Console.WriteLine((t1 & Test.A) > 0); // why this is false ?
Console.ReadKey();
}
}
i want to ask about , why
Test t1 = Test.A | Test.E;
but
Console.WriteLine((t1 & Test.A) > 0);
Thanks....
UPDATE:
Thank you for your comment , and the good design...
*I think I will change the bad design as sonn as quickly!! *
*Thank you all the same . (^^ メ)*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使其工作,您必须确保枚举值设置不同的位,因为您正在执行按位与运算。尝试像这样定义 Test
或者。
To make this work, you have to make sure that the enum-values set different bits since you are doing a bitwise-and operation. Try to define Test like this
Alternatively.
原因是
Test.A | Test.E
的计算结果为-2 | 2 = -2
,因此t1 == Test.A
。现在
t1 &测试.E = -2 & 2=2> 0 和
t1 &测试.A = -2 & -2=-2< 0
The reason is that
Test.A | Test.E
evaluates to-2 | 2 = -2
, sot1 == Test.A
.Now
t1 & Test.E = -2 & 2 = 2 > 0
andt1 & Test.A = -2 & -2 = -2 < 0