枚举的布尔运算 [英] Boolean operations on Enumerations
问题描述
我想在位图上执行各种布尔操作
(FlagsAttribute)
enum类型用于状态机设计,如:
-------------------
[FlagsAttribute]
enum portState {
未知,
开放,
有效,
安全,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
关闭
}
myEnum l_prt_sta;
.... //按位AND测试端口状态
if(l_prt_sta& portState.Active)
{
//做一个有效的端口进程
}
.... //向端口状态添加新状态
>
l_prt_sta + = Requester_trace_cutoff |
Trace_cut off_due_to_protected_status;
-------------------
上述操作(& ;,+ =,|)在C#中没有提供。
不幸的是,Enum类没有值语义来操纵
枚举和状态机值一样。
我可以使用Convert.ToInt32,Convert.ToInt64,ect,
执行布尔运算,
然后重新转换该值。
有没有更好的方法在
枚举上实现布尔运算和赋值?
提前感谢您的回复。
Shawnk
PS。我知道BitArray和BitVector32,但我想使用枚举提供的''命名的
状态'语义。所以我希望''state''同时具有
boolean
向量和枚举语义。
PPS。那么做多个枚举值赋值的最佳方法是什么?
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
.... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
.... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the ''named
state'' semantics that enum provides. So I want the ''state'' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?
推荐答案
Shawnk,
实际上,C#确实有&,+ =和|运营商。只是你需要使用一个返回布尔值的表达式。
。话虽如此,
你需要这样做:
if(l_prt_sta& portState.Active!= 0)
那应该有用。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C# MVP]
- mv*@spam.guard.caspershouse.com
" Shawnk" <嘘**** @ discussions.microsoft.com>在留言中写道
news:68 ********************************** @ microsof t.com ...
Shawnk,
Actually, C# does have the &, +=, and | operators. It''s just that you
need to use an expression that returns a boolean value. That being said,
you need to do this:
if (l_prt_sta & portState.Active != 0)
That should work.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Shawnk" <Sh****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
我想在位图上执行各种布尔操作
(FlagsAttribute)
状态机设计的枚举类型,如:
-------------------
[FlagsAttribute]
enum portState {
未知,
打开,
活动,
安全,
Requester_trace_quesoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
关闭
myEnum l_prt_sta;
... //按位AND测试端口状态
if(l_prt_sta& portState.Active)
{
//执行活动端口过程
}
... //添加新状态到港口州
l_prt_sta + = Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
上述操作(在C#中没有提供&,+ =,|)。不幸的是,Enum类没有值语义来操作
枚举,如同状态机值。
我可以使用Convert.ToInt32,Convert.ToInt64,ect,
执行布尔运算,
然后重新转换该值。
是否有更好的方法来实现布尔操作和分配
枚举?
提前感谢任何回复。
> Shawnk
PS。我知道BitArray和BitVector32,但我想使用枚举提供的
''命名
状态'语义。所以我希望''state''具有
boolean
向量和枚举语义。
PPS。那么进行多个枚举值赋值的最佳方法是什么?
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to
manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on
an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the
''named
state'' semantics that enum provides. So I want the ''state'' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?
有趣。
你知道吗布尔表达式语义背后的推理?
为什么C#在执行简单的时候会有''!=''和''if()''语义
布尔逻辑?
我要求以防万一我应该用我的
代码测试或做什么。
>
非常感谢您的回复。
Shawnk
Nicholas Paldino [.NET / C#MVP]"写道:
Interesting.
Do you know the reasoning behind the boolean expression semantics?
Why does C# have the ''!='' and the ''if()'' semantics when performing simple
boolean logic?
I ask just in case there is something I should be testing for or doing in my
code.
Thanks so much for your response.
Shawnk
"Nicholas Paldino [.NET/C# MVP]" wrote:
Shawnk,
实际上,C#确实有&,+ =和|运营商。只是你需要使用一个返回布尔值的表达式。话虽如此,
你需要这样做:
if(l_prt_sta& portState.Active!= 0)
这应该有效。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
Shawnk <嘘**** @ discussions.microsoft.com>在消息中写道
新闻:68 ********************************** @ microsof t.com。 ..
Shawnk,
Actually, C# does have the &, +=, and | operators. It''s just that you
need to use an expression that returns a boolean value. That being said,
you need to do this:
if (l_prt_sta & portState.Active != 0)
That should work.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Shawnk" <Sh****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
我想在位图上执行各种布尔操作
(FlagsAttribute)
状态机设计的枚举类型,如:
- ------------------
[FlagsAttribute]
enum portState {
未知,
打开,
有效,
安全,
Requester_trace_quesoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
关闭
myEnum l_prt_sta;
... //按位和测试端口状态
如果(l_prt_sta& portState.Active)
//
//执行活动端口过程
}
... //向端口状态添加新状态
l_ prt_sta + = Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
上述操作(在C#中没有提供&,+ =,|)。不幸的是,Enum类没有值语义来操作
枚举,如同状态机值。
我可以使用Convert.ToInt32,Convert.ToInt64,ect,
执行布尔运算,
然后重新转换该值。
是否有更好的方法来实现布尔操作和分配
枚举?
提前感谢任何回复。
> Shawnk
PS。我知道BitArray和BitVector32,但我想使用枚举提供的
''命名
状态'语义。所以我希望''state''具有
boolean
向量和枚举语义。
PPS。那么进行多个枚举值赋值的最佳方法是什么?
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to
manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on
an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the
''named
state'' semantics that enum provides. So I want the ''state'' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?
澄清。我的意思是分配不测试的值。
我理解测试枚举状态如下;
if(target_state& desired_value != 0)
{
//做某事,因为目标状态具有所需的值
}
怎么样有点明智的OR用于赋值(就我的意思而言);
target_state + = additional_value;
target_state = target_state | additional_value;
这些表达式是否需要''if()''上下文才能工作?
非常感谢你的帮助。 />
Shawnk
" Shawnk"写道:
A clarification. I meant for assigment of values not testing.
I understand for the testing the enumeration state as in;
if ( target_state & desired_value != 0)
{
// Do something because the target state has the desired value
}
what about a bit wise OR for assignment (is what I meant) as in;
target_state += additional_value;
target_state = target_state | additional_value;
Will these expressions need the ''if()'' context to work?
Thanks much for your help.
Shawnk
"Shawnk" wrote:
有趣。
你知道布尔表达式语义背后的原因吗?
为什么C#有'' !=''和执行简单布尔逻辑时的''if()''语义?
我要求以防万一我应该测试或做我的<感谢您的回复。
Shawnk
Nicholas Paldino [.NET / C#MVP]"写道:
Interesting.
Do you know the reasoning behind the boolean expression semantics?
Why does C# have the ''!='' and the ''if()'' semantics when performing simple
boolean logic?
I ask just in case there is something I should be testing for or doing in my
code.
Thanks so much for your response.
Shawnk
"Nicholas Paldino [.NET/C# MVP]" wrote:
Shawnk,
实际上,C#确实有&,+ =和|运营商。只是你需要使用一个返回布尔值的表达式。话虽如此,
你需要这样做:
if(l_prt_sta& portState.Active!= 0)
这应该有效。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com
Shawnk <嘘**** @ discussions.microsoft.com>在消息中写道
新闻:68 ********************************** @ microsof t.com。 ..
Shawnk,
Actually, C# does have the &, +=, and | operators. It''s just that you
need to use an expression that returns a boolean value. That being said,
you need to do this:
if (l_prt_sta & portState.Active != 0)
That should work.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Shawnk" <Sh****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
我想在位图上执行各种布尔操作
(FlagsAttribute)
状态机设计的枚举类型,如:
- ------------------
[FlagsAttribute]
enum portState {
未知,
打开,
有效,
安全,
Requester_trace_quesoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
关闭
myEnum l_prt_sta;
... //按位和测试端口状态
如果(l_prt_sta& portState.Active)
//
//执行活动端口过程
}
... //向端口状态添加新状态
l_ prt_sta + = Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
上述操作(在C#中没有提供&,+ =,|)。不幸的是,Enum类没有值语义来操作
枚举,如同状态机值。
我可以使用Convert.ToInt32,Convert.ToInt64,ect,
执行布尔运算,
然后重新转换该值。
是否有更好的方法来实现布尔操作和分配
枚举?
提前感谢任何回复。
> Shawnk
PS。我知道BitArray和BitVector32,但我想使用枚举提供的
''命名
状态'语义。所以我希望''state''具有
boolean
向量和枚举语义。
PPS。那么进行多个枚举值赋值的最佳方法是什么?
I would like to perform various boolean operations on bitmapped
(FlagsAttribute)
enum types for a state machine design as in;
-------------------
[FlagsAttribute]
enum portState {
Unknown,
Open,
Active,
Secure,
Requester_trace_requested,
Requester_trace_cutoff,
Requester_trace_complete,
Requester_trace_valid,
Trace_cutoff_due_to_timeout,
Trace_cutoff_due_to_privacy_rights,
Trace_cutoff_due_to_protected_status
Close
}
myEnum l_prt_sta;
... // Bitwise AND to test port state
if ( l_prt_sta & portState.Active )
{
// Do an active port process
}
... // Add new states to port state
l_prt_sta += Requester_trace_cutoff |
Trace_cutoff_due_to_protected_status;
-------------------
The operations above (&, +=, |) are not provided in C#.
Unfortunately the Enum class does not have the value semantics to
manipulate
the
enumeration as as a state machine value.
I could use Convert.ToInt32, Convert.ToInt64, ect,
perform the boolean operation,
and then reconvert the value back.
Is there a better way to implement boolean operations and assignments on
an
enum?
Thanks ahead of time for any responses.
Shawnk
PS. I know about BitArray and BitVector32 but I would like to use the
''named
state'' semantics that enum provides. So I want the ''state'' to have both
boolean
vector and enumeration semantics.
PPS. ALso what is the best way to do multiple enum value assignments?
这篇关于枚举的布尔运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!