简体   繁体   English

C#运算符按位

[英]C# Operators Bitwise

I've been looking over some game programming code and have seen enums as in: 我一直在查看一些游戏编程代码,并看到如下枚举:

[Flags]
public enum CollisionCategories
{
    Cat1 = (1 << 0),
    Cat2 = (1 << 1),
    Cat3 = (1 << 2),
    Cat4 = (1 << 3),
    ...
}

Now, would this not be the same as just setting each item like 1, 2, 4, 8, ... ? 现在,这与仅将每个项目设置为1、2、4、8,...不同吗? I've seen the later as well. 我也看过后来的。 I know doing something like string s = string.Empty is better than string s = "" as far as performance goes but not sure about the enum. 就性能而言,我知道这样做像string s = string.Emptystring s = ""更好,但不确定枚举。

Any thoughts? 有什么想法吗?

Thanks much, 非常感谢,

David 大卫

You are correct about the values that are stored. 您对存储的值是正确的。 It would not make a difference in performance, so it is a readability issue that may make more sense in the context of the code. 这不会影响性能,因此这是一个可读性问题,在代码上下文中可能更有意义。

It's just easier when you get to the larger numbers. 当您获得更大的数字时,这会更加容易。

I'm sure that (1<< 24) is simpler than going to your calculator, calculating it, and pasting. 我敢肯定(1 << 24)比去计算器,计算和粘贴要简单。

For some people, setting them this way is just cleaner than the typical C raw hex/decimal number initialization. 对于某些人来说,以这种方式设置它们比典型的C原始十六进制/十进制数初始化更干净。 (0x1,0x4000,etc) Since the compiler recognizes a literal and turns them into plain numbers, there's no performance drawback, it's just a style matter. (0x1,0x4000等)由于编译器可以识别文字并将其转换为纯数字,因此没有性能上的缺陷,这只是样式问题。

Yes it is the same. 是的,是一样的。

I personally would use 1,2,4,8... because I think these numbers are so much well known that no one can misunderstand what's going on. 我个人将使用1,2,4,8 ...,因为我认为这些数字众所周知,因此没有人会误会发生了什么。

Performance is the same. 性能是一样的。 Code size is the same. 代码大小是相同的。

Yes, it is the same. 是的,是一样的。 It's just so the code is more readable. 只是这样,代码才更具可读性。

I think String.Empty is equivalent to "" the compiler will recognise the literal and convert it..... Anyway, it's the same for you enum, it's just a question of style and in the case of the enum it makes it 100% explicit that these are bit flags I suppose. 我认为String.Empty等效于“”,编译器将识别文字并将其转换.....无论如何,对于您的枚举是相同的,这只是样式问题,在枚举的情况下,它将变为100 %明确表示这些是我想的位标志。

Or maybe the coder wasn't confident with his powers of 2, and couldn't be bothered to work them out. 或者,也许编码员对他的2的幂数不满意,也不会费心去解决它们。

Performance is not an issue here. 性能在这里不是问题。 Enum is compiled as a constant so it is the compiler that is doing the job hence no difference in runtime performance. 枚举被编译为常量,因此是由编译器执行的,因此运行时性能没有差异。

Alternative could have been using the power but since that involves code, it would not work: 可以使用替代功能,但是由于涉及代码,因此无法使用:

enum MyEnum{
  Val1 = Math.Pow(1,2) // throws compile error
}

Error 1 The expression being assigned to 'ConsoleApplication1.Program.MyEnum.Val1' must be constant C:\\Users\\Aliostad\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\Program.cs 130 11 ConsoleApplication1 错误1分配给'ConsoleApplication1.Program.MyEnum.Val1'的表达式必须为常量 C:\\ Users \\ Aliostad \\ Documents \\ Visual Studio 2010 \\ Projects \\ ConsoleApplication1 \\ ConsoleApplication1 \\ Program.cs 130 11 ConsoleApplication1

Enum values must be constants. 枚举值必须是常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM