flags

C# Enums with Flags Attribute

谁说我不能喝 提交于 2019-12-03 11:58:35
I was wondering if Enums with Flag attribute are mostly used for Bitwise operations why not the compilers autogenerate the values if the enum values as not defined. For eg. [Flags] public enum MyColor { Yellow = 1, Green = 2, Red = 4, Blue = 8 } It would be helpful if the values 1,2,4,8 are autogenerated if they are not assigned. Would like to know your thoughts on this. This is a little easier: [Flags] public enum MyColor { Yellow = 1<<0, Green = 1<<1, Red = 1<<2, Blue = 1<<3 } They probably could, but with a compiler of this size they have to take into consideration the time and resources

Why use flags+bitmasks rather than a series of booleans?

五迷三道 提交于 2019-12-03 10:50:05
问题 Given a case where I have an object that may be in one or more true/false states, I've always been a little fuzzy on why programmers frequently use flags+bitmasks instead of just using several boolean values. It's all over the .NET framework. Not sure if this is the best example, but the .NET framework has the following: public enum AnchorStyles { None = 0, Top = 1, Bottom = 2, Left = 4, Right = 8 } So given an anchor style, we can use bitmasks to figure out which of the states are selected.

Is it possible to only test specific functions with doctest in a module?

ε祈祈猫儿з 提交于 2019-12-03 10:35:39
I am trying to get into testing in Python using the doctest module. At the moment I do Write the tests for the functions. implement the functions code. If Tests pass, write more tests and more code. When the function is done move on to the next function to implement. So after 3 or 4 (independent) functions in the same module with many tests I get a huge output by doctest. And it is a little annoysing. Is there a way to tell doctest "don't test functions a() , b() and c() ", so that it runs only the unmarked functions? I only found the doctest.SKIP flag, which is not sufficient for my needs. I

Flags in a database rows, best practices

本小妞迷上赌 提交于 2019-12-03 10:23:32
I am asking this out of a curiosity. Basically my question is when you have a database which needs a row entry to have things which act like flags, what is the best practice? A good example of this would be the badges on stack overflow, or the operating system field in bugzilla. Any subset of the flags may be set for a given entry. Usually, I do c and c++ work, so my gut reaction is to use an unsigned integer field as a set of bits which can be flipped... But i know that isn't a good solution for several reasons. The most obvious of which is scale-ability, there will be a hard upper limit on

Flags, enum (C)

早过忘川 提交于 2019-12-03 09:04:53
问题 I'm not very used to programming with flags, but I think I just found a situation where they'd be useful: I've got a couple of objects that register themselves as listeners to certain events. What events they register for is dependent on a variable that is sent to them when they are constructed. I think a nice way to do this would be to send bitwise OR connected variables, like such: TAKES_DAMAGE | GRABBABLE | LIQUID, etc. Then, in the constructor, the object can check what flags are set and

Testing a [Flags] enum value for a single value

心已入冬 提交于 2019-12-03 07:02:18
If I have an enum that's marked with [Flags] , is there a way in .NET to test a value of this type to see if it only contains a single value? I can get the result I want using bit-counting, but I'd rather use built-in functions if possible. When looping through the enum values dynamically, Enum.GetValues() returns the combination flags as well. Calling that function on the enum in the following example returns 4 values. However, I don't want the value combinations included in the inner algorithm. Testing individual enum values for equality is out, since the enum could potentially contain many

Why is the Carry Flag set during a subtraction when zero is the minuend?

白昼怎懂夜的黑 提交于 2019-12-03 06:31:26
At the moment I write my own little library for arithmetic and logical operations for very big unsigned integers. To improve performance I decided to implement some functions in assembly. So here is my question. While subtracting two unsigned integers the Carry Flag is set when I subtract any number from 0. But why is the Carry Flag set in this situation? The Carry Flag is only set when an overflow occurs, but if I subtract any number from zero I don't get an overflow. Or I am wrong? Carry flag is carry or borrow out of the Most Significant bit (MSb): CF (bit 0) Carry flag — Set if an

Comparing enum flags in C#

冷暖自知 提交于 2019-12-03 05:46:07
I need to detect if a flag is set within an enum value, which type is marked with the Flag attribute. Usually it is made like that: (value & flag) == flag But since I need to do this by generic (sometimes at runtime I event have only an "Enum" reference. I can not find an easy way to use the & operator. At the moment I make it like this: public static bool IsSet<T>(this T value, T flags) where T : Enum { Type numberType = Enum.GetUnderlyingType(typeof(T)); if (numberType.Equals(typeof(int))) { return BoxUnbox<int>(value, flags, (a, b) => (a & b) == b); } else if (numberType.Equals(typeof(sbyte

Android: Intent Flag to destroy activity and start new one

人盡茶涼 提交于 2019-12-03 04:47:58
So I have a Login Activity This Activity inflates a login.xml layout which has a USER_NAME and PASSWORD EditText Views, when I enter the Username and Password and click the Login Button I start a new Activity. The new Activity has a Logout button which basically just starts the previous Activity like so: Intent loginIntent = new Intent(getActivity(), Login.class); loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); getActivity().startActivity(loginIntent); According to the Android Documentation the flag does the following: http://developer.android.com/reference/android/content/Intent.html

Switch on Enum (with Flags attribute) without declaring every possible combination?

谁都会走 提交于 2019-12-03 04:04:43
问题 how do i switch on an enum which have the flags attribute set (or more precisely is used for bit operations) ? I want to be able to hit all cases in a switch that matches the values declared. The problem is that if i have the following enum [Flags()]public enum CheckType { Form = 1, QueryString = 2, TempData = 4, } and I want to use a switch like this switch(theCheckType) { case CheckType.Form: DoSomething(/*Some type of collection is passed */); break; case CheckType.QueryString: