为什么要使用的<<运营商在枚举声明? [英] Why would someone use the << operator in an enum declaration?

查看:89
本文介绍了为什么要使用的<<运营商在枚举声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的代码我目前在我的项目,发现是这样的:

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 << 01,
    Maybe    = 1 << 02,
    ........
}

<< 操作数是移位操作,而移动在第二个操作数指定的位数留下的第一个操作数

The << operand is the shift operand, which shifts the first operand left by the number bits specified in the second operand.

但为什么会有人在枚举声明中使用这个?

But why would someone use this in an enum declaration?

推荐答案

这允许你做这样的事情:

This allows you to do something like this:

var myEnumValue = MyEnum.open | MyEnum.close;



无需计算的2倍数位值。

without needing to count bit values of multiples of 2.

(像这样):

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

这篇关于为什么要使用的&lt;&LT;运营商在枚举声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆