C# 枚舉運用"位"操作和"或"操作

來源:互聯網
上載者:User

標籤:style   blog   color   使用   io   ar   cti   div   

定義:

    /// <summary>    /// The js function type(the same as name).    /// </summary>    [Flags]    public enum CallJSFunctionTypes    {        None = 0,        ResetFixedBar = 1 << 1,        ResetRequiredField = 1 << 2,        SetValidateSuccessTextBoxStyle = 1 << 3,        SetValidateFailTextBoxStyle = 1 << 4,        ResizeSummary = 1 << 5        //,All = 1 << 5 - 1    }

使用:

//可以先給個初始值.CallJSFunctionTypes JSFunctions = CallJSFunctionTypes.None;//...//可以這樣賦值, 想包含什麼意義, 就用"與"疊加. JSFunctions = CallJSFunctionTypes.ResetFixedBar | CallJSFunctionTypes.ResetRequiredField | CallJSFunctionTypes.ResizeSummary;//...//判斷是否包含某個意義if ((JSFunctions & CallJSFunctionTypes.ResetFixedBar) == CallJSFunctionTypes.ResetFixedBar){      //Do something.}

原理:

Int32 是 4位元組32位二進位
None = 0,                                               

即 0000 0000 0000 0000 0000 0000 0000 0000


ResetFixedBar = 1 << 1,                           

即 0000 0000 0000 0000 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0000 0010


ResetRequiredField = 1 << 2,                   

即 0000 0000 0000 0000 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0000 0100


SetValidateSuccessTextBoxStyle = 1 << 3,

即 0000 0000 0000 0000 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0000 1000


SetValidateFailTextBoxStyle = 1 << 4,       

即 0000 0000 0000 0000 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0001 0000


ResizeSummary = 1 << 5,                        

即 0000 0000 0000 0000 0000 0000 0000 0001 -> 0000 0000 0000 0000 0000 0000 0010 0000


All = 1 << 5 - 1                                        

即 0000 0000 0000 0000 0000 0000 0010 0000 -> 0000 0000 0000 0000 0000 0000 0001 1111

 

賦值的時候:

ResetFixedBar|SetValidateSuccessTextBoxStyle|ResizeSummary

0000 0000 0000 0000 0000 0000 0000 0010

0000 0000 0000 0000 0000 0000 0000 1000

0000 0000 0000 0000 0000 0000 0010 0000

____________________________________

0000 0000 0000 0000 0000 0000 0010 1010

 

判斷的時候:

判斷有沒有ResetFixedBar, 相"與"(&)

0000 0000 0000 0000 0000 0000 0010 1010

0000 0000 0000 0000 0000 0000 0000 0010

____________________________________

0000 0000 0000 0000 0000 0000 0000 0010 即ResetFixedBar, 即存在ResetFixedBar

 

判斷有沒有ResetRequiredField, 相"與"(&)

0000 0000 0000 0000 0000 0000 0010 1010

0000 0000 0000 0000 0000 0000 0000 0100

____________________________________

0000 0000 0000 0000 0000 0000 0000 0000 即不存在ResetFixedBar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.