Bit fields in C#

I have a structure which I need to populate and write to disk (several actually). An example is: byte-6 bit0 - original_or_copy bit1 - copyright bit2 - data_alignment_indicator bit3 - PES_priority bit4-bit5 - PES_scrambling control. bit6-bit7 - reserved In CI might do something like the following: struct PESHeader { unsigned reserved:2; unsigned scrambling_control:2;

C#中的位域

我有一个需要填充和写入磁盘的结构(实际上有几个)。 一个例子是: byte-6 bit0 - original_or_copy bit1 - copyright bit2 - data_alignment_indicator bit3 - PES_priority bit4-bit5 - PES_scrambling control. bit6-bit7 - reserved 在CI中可能会做类似如下的事情: struct PESHeader { unsigned reserved:2; unsigned scrambling_control:2; unsigned priority:1; unsigned data_ali

Why do enum permissions often have 0, 1, 2, 4 values?

Why are people always using enum values like 0, 1, 2, 4, 8 and not 0, 1, 2, 3, 4 ? Has this something to do with bit operations, etc.? I would really appreciate a small sample snippet on how this is used correctly :) [Flags] public enum Permissions { None = 0, Read = 1, Write = 2, Delete = 4 } Because they are powers of two and I can do this: var permissions = Permissi

为什么枚举权限通常具有0,1,2,4的值?

为什么人们总是喜欢用枚举值0, 1, 2, 4, 8 ,而不是0, 1, 2, 3, 4 ? 这是否与位操作等有关? 我真的很感激一个关于如何正确使用它的小示例代码:) [Flags] public enum Permissions { None = 0, Read = 1, Write = 2, Delete = 4 } 因为他们是两个权力,我可以这样做: var permissions = Permissions.Read | Permissions.Write; 也许以后...... if( (permissions & Permissions.Write) == Per

c# flag comparison "only contain" a set

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers First off, when defining a flags enum, each flag should represent a single bit in the enum: enum X { a = 1, b = 2, c = 4, d = 8 } This allows you to combine flags as well: CandD = 12, //has both c and d flags set. Or if you have a lot of them: enum X { a = 1 << 0, b = 1 <

c#标志比较“只包含”一组

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 首先,在定义一个标志枚举时,每个标志应该代表枚举中的一个位: enum X { a = 1, b = 2, c = 4, d = 8 } 这可以让你结合标志: CandD = 12, //has both c and d flags set. 或者如果你有很多他们: enum X { a = 1 << 0, b = 1 << 1, c = 1 << 2, d = 1 << 3, ... CAndD = c | d } 您可以使用

How to implement this using the bitwise & (and)?

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers Typical patterns for testing bit flags are // Entire key match if (returned_value & value_to_test == value_to_test) { ... } // Partial key match if (returned_value & value_to_test != 0) { ... } Eg if you want to test if pocket #3 is full: if (returned_value & POCKET.P

如何使用按位&(和)来实现这个?

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 测试位标志的典型模式是 // Entire key match if (returned_value & value_to_test == value_to_test) { ... } // Partial key match if (returned_value & value_to_test != 0) { ... } 例如,如果你想测试3号口袋是否满了: if (returned_value & POCKET.P3_FULL == POCKET.P3_FULL) { ... } 您可以通过|组合标

C# flags vs sample enums

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers The core difference between flags and classic enums is that enum holds only one value: A or B or C while flags are meant to hold sets of values: A or B or C or AB or AC or BC or ABC. You may want to read about it here: MSDN

C#标志与示例枚举

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 标志与经典枚举的核心区别在于枚举只保存一个值:A或B或C,而标志用于保存一组值:A或B或C或AB或AC或BC或ABC。 你可能想在这里阅读它:MSDN

What are [..] called above enum / class definition?

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers What does square bracket [] mean in the below code? 2 answers what is [] brackets in .net? [duplicate] 7 answers It's a class attribute. There are also method attributes for example. You can even write your own They are usually used to define meta information or behaviour abou

什么是[?]以上枚举/类定义?

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 在下面的代码中,方括号[]是什么意思? 2个答案 .net中的[]括号是什么? [复制] 7个答案 这是一个班级属性。 例如也有方法属性。 你甚至可以自己写 它们通常用于定义关于类或方法的元信息或行为,并且可以使用反射来读取它们。

C# enum, double previous enum value

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers The reason that you need this is that the flags enum is represented as a binary number. Each value that is a power of 2 (1,2,4,8,16 etc.) corresponds to a different index in the binary number: 2 -> 10 4 -> 100 8 -> 1000 This is essential so that you can determine which flag

C#枚举,双前一个枚举值

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 你需要这个的原因是flags枚举被表示为一个二进制数。 每个值为2(1,2,4,8,16等)的幂对应于二进制数中的不同索引: 2 -> 10 4 -> 100 8 -> 1000 这是至关重要的,这样您可以确定给定值实际包含哪些标志。 例如,如果你的类的枚举值是1110它的标志值是2,4和8。 如果您使用的值不是2的幂,那么在执行按位和&时,c#不

How to maintain in an enum a list of enums

Possible Duplicate: Enum Flags Attribute I've seen in Windows Forms that when you use a control which contains Anchor, you can set several enums at the same time. I was trying to do that, but I haven't accomplished yet: enum Foo { A, B, C } Foo foo = A | B; // here just receive the last one Is possible doing this? Use the Flags attribute. Flags: [Flags] enum Da

如何在枚举中保留一个枚举列表

可能重复: 枚举标志属性 我在Windows Forms中看到,当您使用包含Anchor的控件时,您可以同时设置多个枚举。 我试图这样做,但我还没有完成: enum Foo { A, B, C } Foo foo = A | B; // here just receive the last one 有可能这样做吗? 使用标志属性。 标志: [Flags] enum Days2 { None = 0x0, Sunday = 0x1, Monday = 0x2, Tuesday = 0x4, Wednesday = 0x8, Thursday =

Integer to enum casting in C#

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers Let's say your enum is this: enum E { A = 1 B = 2 C = 4 D = 8 } If you translate each value to it's binary value, you get: A = 0001 B = 0010 C = 0100 D = 1000 Now let's say you try to convert 12 , 1100 in binary, to this enum : E enumTest = (E)12; It will ou

整数在C#中枚举铸造

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 假设你的enum是这样的: enum E { A = 1 B = 2 C = 4 D = 8 } 如果您将每个值转换为二进制值,您会得到: A = 0001 B = 0010 C = 0100 D = 1000 现在让我们假设你尝试转换12 , 1100二进制,这个enum : E enumTest = (E)12; 它会输出C|D | 作为OR运算符, C|D意味着0100 OR 1000 ,它给了我们1100或十进制的12 。

c# check enum is contained in options

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers The simplest way is to use & : if ((available & me) != 0) You can use 0 here as there's an implicit conversion from the constant 0 to any enum, which is very handy. Note that your enum should be defined using the Flags attribute and appropriate bit-oriented values though: [F

c#check enum包含在选项中

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 最简单的方法是使用& : if ((available & me) != 0) 你可以在这里使用0,因为有从常量0到任何枚举的隐式转换,这非常方便。 请注意,您的枚举应该使用Flags属性和适当的位导向值来定义: [Flags] public enum Fruits { Apple = 1 << 0, Orange = 1 << 1, Grape = 1 << 2, Ananas = 1 <<