C# 值为 0x0001 的枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1173487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
enum with value 0x0001?
提问by Broken Link
I have an enum declaration like this:
我有一个像这样的枚举声明:
public enum Filter
{
a = 0x0001;
b = 0x0002;
}
What does that mean? They are using this to filter an array.
这意味着什么?他们正在使用它来过滤数组。
采纳答案by samoz
It just means that if you do Filter->a, you get 1. Filter->b is 2.
这只是意味着如果你做 Filter->a,你得到 1。Filter->b 是 2。
The weird hex notation is just that, notation.
奇怪的十六进制符号就是那个符号。
EDIT: Since this is a 'filter' the hex notation makes a little more sense.
编辑:由于这是一个“过滤器”,因此十六进制表示法更有意义。
By writing 0x1, you specify the following bit pattern:
通过写入 0x1,您可以指定以下位模式:
0000 0001
And 0x2 is:
0x2 是:
0000 0010
This makes it clearer on how to use a filter.
这样可以更清楚地了解如何使用过滤器。
So for example, if you wanted to filter out data that has the lower 2 bits set, you could do:
因此,例如,如果您想过滤掉设置了低 2 位的数据,您可以执行以下操作:
Filter->a | Filter->b
which would correspond to:
这将对应于:
0000 0011
The hex notation makes the concept of a filter clearer (for some people). For example, it's relatively easy to figure out the binary of 0x83F0 by looking at it, but much more difficult for 33776 (the same number in base 10).
十六进制表示法使过滤器的概念更清晰(对某些人而言)。例如,通过查看来计算 0x83F0 的二进制相对容易,但对于 33776(以 10 为基数的相同数字)要困难得多。
回答by Jon Skeet
It means they're the integer values assigned to those names. Enums are basically just named numbers. You can cast between the underlying type of an enum and the enum value.
这意味着它们是分配给这些名称的整数值。枚举基本上只是命名数字。您可以在枚举的基础类型和枚举值之间进行转换。
For example:
例如:
public enum Colour
{
Red = 1,
Blue = 2,
Green = 3
}
Colour green = (Colour) 3;
int three = (int) Colour.Green;
By default an enum's underlying type is int
, but you can use any of byte
, sbyte
, short
, ushort
, int
, uint
, long
or ulong
:
默认情况下,一个枚举的基础类型int
,但是你可以使用任何的byte
,sbyte
,short
,ushort
,int
,uint
,long
或ulong
:
public enum BigEnum : long
{
BigValue = 0x5000000000 // Couldn't fit this in an int
}
回答by Polaris878
Those look like they are bit masks of some sort. But their actual values are 1 and 2...
那些看起来像是某种位面罩。但它们的实际值是 1 和 2 ...
You can assign values to enums such as:
您可以为枚举分配值,例如:
enum Example {
a = 10,
b = 23,
c = 0x00FF
}
etc...
等等...
回答by JP Alioto
Those are literal hexadecimal numbers.
这些是文字十六进制数字。
回答by Kredns
It could mean anything. We need to see more code then that to be able to understand what it's doing.
它可能意味着任何事情。我们需要看到更多的代码才能理解它在做什么。
0x001
is the number 1
. Anytime you see the 0x
it means the programmer has entered the number in hexadecimal.
0x001
是数字1
。任何时候你看到0x
它都意味着程序员已经输入了十六进制的数字。
回答by Brian
Using Hexidecimal notation like that usually indicates that there may be some bit manipulation. I've used this notation often when dealing with this very thing, for the very reason you asked this question - this notation sort of pops out at you and says "Pay attention to me I'm important!"
使用这样的十六进制表示法通常表示可能存在一些位操作。在处理这件事时,我经常使用这个符号,因为你问这个问题的原因——这种符号突然出现在你身上,说“注意我,我很重要!”
回答by Ankur
Well we can use integers infact we can avoid any as the default nature of enum assigns 0 to its first member and an incremented value to the next available member. Many developers use this to hit two targets with one bow.
好吧,我们可以使用整数,事实上我们可以避免任何整数,因为枚举的默认性质将 0 分配给它的第一个成员,并将递增的值分配给下一个可用成员。许多开发人员使用它来一箭射中两个目标。
- Complicate the code making it difficult to understand
- Faster the performance as hex codes are nearer to binary one
- 代码复杂,难以理解
- 由于十六进制代码更接近二进制代码,因此性能更快
my view is if we are still using why we are in fourth generation language just move to binary again
我的观点是,如果我们仍在使用为什么我们使用第四代语言,只需再次使用二进制
but its quite better technique to play with bits and encryption/decryption process
但它更好的技术来玩位和加密/解密过程
回答by Guffa
It's not clear what it is that you find unclear, so let's discuss it all:
不清楚你觉得不清楚的是什么,所以让我们讨论一下:
The enum values have been given explicit numerical values. Each enum value is always represented as a numerical value for the underlying storage, but if you want to be sure what that numerical value is you have to specify it.
枚举值已被赋予明确的数值。每个枚举值始终表示为底层存储的数值,但如果您想确定该数值是什么,则必须指定它。
The numbers are written in hexadecimal notation, this is often used when you want the numerical values to contain a single set bit for masking. It's easier to see that the value has only one bit set when it's written as 0x8000 than when it's written as 32768.
数字以十六进制表示法书写,当您希望数值包含一个用于屏蔽的设置位时,通常会使用这种表示法。与写入 32768 相比,更容易看出该值在写入 0x8000 时仅设置了一位。
In your example it's not as obvious as you have only two values, but for bit filtering each value represents a single bit so that each value is twice as large as the previous:
在您的示例中,它不像您只有两个值那么明显,但是对于位过滤,每个值代表一个位,因此每个值是前一个值的两倍:
public enum Filter {
First = 0x0001,
Second = 0x0002,
Third = 0x0004,
Fourth = 0x0008
}
You can use such an enum to filter out single bits in a value:
您可以使用这样的枚举来过滤掉值中的单个位:
If ((num & Filter.First) != 0 && (num & Filter.Third) != 0) {
Console.WriteLine("First and third bits are set.");
}
回答by Antoine Meltzheim
Main reason is :
主要原因是:
It is easyer to read hex notation when writing numbers such as : "2 to the power of x" is needed.
在编写数字时更容易阅读十六进制表示法,例如:需要“2 的 x 次方”。
To use enum type as bit flag, we need to increment enum values by power of 2 ... 1,2,4,8,16,32,64, etc. To keep it readable, hex notation is used.
要将枚举类型用作位标志,我们需要按 2 的幂增加枚举值 ... 1,2,4,8,16,32,64 等。为了保持可读性,使用了十六进制表示法。
Ex : 2^10 is 0x10000 in hex (neat and clean), but it is written 65536 in classical decimal notation ... Same for 0x200 (hex notation) and 512. (2^9)
例如:2^10 是十六进制的 0x10000(整洁干净),但它是用经典十进制表示法编写的 65536 ... 0x200(十六进制表示法)和 512 相同。(2^9)