Why would someone use the << operator in an enum declaration?

前端 未结 7 670
悲&欢浪女
悲&欢浪女 2020-12-29 19:13

I was looking at the code I have currently in my project and found something like this:

public enum MyEnum
{
    open     = 1 << 00,
    close    = 1 &         


        
7条回答
  •  天涯浪人
    2020-12-29 19:30

    This allows you to do something like this:

    var myEnumValue = MyEnum.open | MyEnum.close;
    

    without needing to count bit values of multiples of 2.

    (like this):

    public enum MyEnum
    {
        open     = 1,
        close    = 2,
        Maybe    = 4,
        ........
    }
    

提交回复
热议问题