• C# 枚举运用"位"操作和"或"操作


    定义:

        /// <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 << 6 - 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 << 6 - 1                                        

    即 0000 0000 0000 0000 0000 0000 0010 0000 -> 0000 0000 0000 0000 0000 0000 0011 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

  • 相关阅读:
    OOM框架AutoMapper基本使用(2)
    windows 下查看运行进程的命令行参数
    如何用英语打开 Visual Studio 安装包
    qt源代码阅读
    “listening” to file changes in C/C++ (on Windows)
    The Definitive C++ Book Guide and List
    Debug DLLs in Visual Studio (C#, C++, Visual Basic, F#)
    CRT Debug Heap Details
    QStringLiteral
    13.锁的应用
  • 原文地址:https://www.cnblogs.com/xachary/p/3897708.html
Copyright © 2020-2023  润新知