bitflags

Iterate through values in @IntDef, @StringDef or any @Def class

▼魔方 西西 提交于 2019-12-09 16:35:41
问题 Consider this class: public class MyClassOfMystery { public static final int NO_FLAGS = ~0; public static final int FIRST_FLAG = 1; public static final int SECOND_FLAG = 1 << 1; public static final int THIRD_FLAG = 1 << 2; public static final int FOURTH_FLAG = 1 << 3; @Retention(RetentionPolicy.SOURCE) @IntDef(flag = true, value = {NO_FLAGS, FIRST_FLAG, SECOND_FLAG, THIRD_FLAG, FOURTH_FLAG}) public @interface MysteryFlags { } ... set flags, get flags, and use flags stuff. } I have often

Creating bitflag variables with large amounts of flags or how to create large bit-width numbers

烈酒焚心 提交于 2019-12-07 14:52:38
问题 Lets say I have an enum with bitflag options larger than the amount of bits in a standard data type: enum flag_t { FLAG_1 = 0x1, FLAG_2 = 0x2, ... FLAG_130 = 0x400000000000000000000000000000000, }; This is impossible for several reasons. Enums are max size of 128 bits (in C/gcc on my system from experimentation), single variables are also of max size 128 bits etc. In C you can't perform bitwise operations on arrays, though in C++ I suppose you could overload bitwise operators to do the job

Extend Enum with flag methods?

守給你的承諾、 提交于 2019-12-06 09:55:44
I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed. What I think would be really helpful though is an extension to SET a single flag! I have many situations where I need to set the flag values individually. I want an extension method with this signature: enumVariable.SetFlag(EnumType.SingleFlag, true); OR possibly: enumVariable.SetFlag<EnumType>(EnumType.SingleFlag, true); Today I found a solution on http://hugoware.net/blog/enums-flags-and-csharp . Thanks Hugo!

Creating bitflag variables with large amounts of flags or how to create large bit-width numbers

别来无恙 提交于 2019-12-05 22:20:23
Lets say I have an enum with bitflag options larger than the amount of bits in a standard data type: enum flag_t { FLAG_1 = 0x1, FLAG_2 = 0x2, ... FLAG_130 = 0x400000000000000000000000000000000, }; This is impossible for several reasons. Enums are max size of 128 bits (in C/gcc on my system from experimentation), single variables are also of max size 128 bits etc. In C you can't perform bitwise operations on arrays, though in C++ I suppose you could overload bitwise operators to do the job with a loop. Is there any way in C other than manually remembering which flags go where to have this work

Are enums the canonical way to implement bit flags?

放肆的年华 提交于 2019-12-05 15:30:25
问题 Currently I'm using enums to represent a state in a little game experiment. I declare them like so: namespace State { enum Value { MoveUp = 1 << 0, // 00001 == 1 MoveDown = 1 << 1, // 00010 == 2 MoveLeft = 1 << 2, // 00100 == 4 MoveRight = 1 << 3, // 01000 == 8 Still = 1 << 4, // 10000 == 16 Jump = 1 << 5 }; } So that I can use them this way: State::Value state = State::Value(0); state = State::Value(state | State::MoveUp); if (mState & State::MoveUp) movement.y -= mPlayerSpeed; But I'm

Iterate through values in @IntDef, @StringDef or any @Def class

我怕爱的太早我们不能终老 提交于 2019-12-04 05:07:52
Consider this class: public class MyClassOfMystery { public static final int NO_FLAGS = ~0; public static final int FIRST_FLAG = 1; public static final int SECOND_FLAG = 1 << 1; public static final int THIRD_FLAG = 1 << 2; public static final int FOURTH_FLAG = 1 << 3; @Retention(RetentionPolicy.SOURCE) @IntDef(flag = true, value = {NO_FLAGS, FIRST_FLAG, SECOND_FLAG, THIRD_FLAG, FOURTH_FLAG}) public @interface MysteryFlags { } ... set flags, get flags, and use flags stuff. } I have often created something like this, and found that it would be useful to be able to iterate through all flags

What Does the [Flags] Attribute Really Do?

我与影子孤独终老i 提交于 2019-11-27 22:43:04
What does applying [Flags] really do? I know it modifies the behavior of Enum.ToString , but does it do anything else? (e.g. Different compiler or runtime behavior, etc.) Edit : Yeah, I'm aware that it documents the fact that the enum is intended to be used as bitwise flags, and that it's more logical to apply it to bit flags. I was asking more about concrete behavior changes though, not general programming practices. From an MSDN article : It is interesting to note that when Flags is specified, Parse and Format methods feature advanced capabilities. Likewise, the Parse method can successfully