转:位操作才简单好用!

C# Implementation



Defining the Flags:
To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags.

[Flags] private enum Buttons : int
{
···Ok = 1, Cancel = 2, Retry = 4, Help = 8
}

Tip: An "All" item could be added to the list of items in the enumeration as follows: All = Ok | Cancel | Retry | Help


Setting flags ON:
To set multiple flags, concatenate the desired flags using the bitwise OR symbol "|":

Buttons buttons;
buttons = Buttons.Ok | Buttons.Cancel;


Setting flags OFF:

buttons &= ~Buttons.Cancel;


Testing to see if a certain flag is set:

if ((buttons&Buttons.Ok)==Buttons.Ok)
···Console.WriteLine("Ok");



VB.NET Implementation


(This section in the same as the C# section, the examples are just in VB.)

Defining the Flags:
To start of with, declare an enum to list all the possible flags. Two things are important when declaring the enum. The first thing you will probably notice is the [Flags] attribute. This is necessary in order to indicate that the enumeration should be treated as a set of flags. The second important thing is assigning a value to each of the items in the enum. The first value should be 1, then just double the value for each consecutive item. The integer type in .NET can store up to 32 flags.

<Flags()> _
Private Enum Buttons As Integer
···Ok = 1
···Cancel = 2
···Retry = 4
···Help = 8
End Enum

Tip: An "All" item could be added to the list of items in the enumeration as follows: All = Ok And Cancel And Retry And Help


Setting flags ON:
To set multiple flags, concatenate the desired flags using the Or keyword:

Dim btns As Buttons
btns = Buttons.Ok Or Buttons.Cancel


Setting flags OFF:

btns = btns And Not Buttons.Cancel


Testing to see if a certain flag is set:

If (btns And Buttons.Ok) = Buttons.Ok Then
···Console.WriteLine("Ok")
End If



TSQL Implementation



Of course there are no enumeration constructs in TSQL, so the script required to implement flags is a bit rougher than in .NET, but it works just as well. This section anticipates that you would have already looked at one of the two sections above before you work through this one...

Setting flag 1 ON:

declare @MyFlags int
select @MyFlags = IsNull(@MyFlags, 0) | 1


Setting flag 1 OFF:

select @MyFlags = IsNull(@MyFlags, 0) ^ 1



Testing to see if flag 1 is set:

if (IsNull(@MyFlags, 0) & 1) <> 0
···select 'Yes'
else
···select 'No'
posted @ 2009-02-12 15:38  new 维生素C.net()  阅读(284)  评论(0编辑  收藏  举报