Questions tagged [flags]

Flags are atomic data structures used to identify state in a program.

A flag field is an integer interpreted as a sequence of boolean bits, each called a flag. A single flag has two possible states: 0, representing false or off, and 1, representing true or on.

This tag can be used for problems related to .

Source

Wikipedia

1329 questions
1545
votes
13 answers

What does the [Flags] Enum Attribute mean in C#?

From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what exactly the [Flags] attribute does. Anyone have a good…
Brian Leahy
  • 32,433
  • 10
  • 42
  • 60
393
votes
5 answers

Compiling C++11 with g++

I'm trying to update my C++ compiler to C++11. I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu…
Rontogiannis Aristofanis
  • 8,185
  • 8
  • 37
  • 58
207
votes
10 answers

Most common C# bitwise operations on enums

For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have. For example: flags = flags | FlagsEnum.Bit4; …
steffenj
  • 7,729
  • 10
  • 33
  • 41
197
votes
17 answers

How to check if any flags of a flag combination are set?

Let's say I have this enum: [Flags] enum Letters { A = 1, B = 2, C = 4, AB = A | B, All = A | B | C, } To check if for example AB is set I can do this: if((letter & Letters.AB) == Letters.AB) Is there a simpler way to…
Svish
  • 138,188
  • 158
  • 423
  • 589
177
votes
4 answers

Bash if statement with multiple conditions throws an error

I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script: my_error_flag=0 my_error_flag_o=0 do something..... if [[ "$my_error_flag"=="1" ||…
Simply_me
  • 2,340
  • 2
  • 14
  • 24
161
votes
7 answers

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…
Pascal
  • 10,107
  • 18
  • 83
  • 180
159
votes
3 answers

How can I remove a flag in C?

There is a variable that holds some flags and I want to remove one of them. But I don't know how to remove it. Here is how I set the flag. my.emask |= ENABLE_SHOOT;
Aaron de Windt
  • 13,661
  • 12
  • 44
  • 58
158
votes
11 answers

How to Compare Flags in C#?

I have a flag enum below. [Flags] public enum FlagTest { None = 0x0, Flag1 = 0x1, Flag2 = 0x2, Flag3 = 0x4 } I cannot make the if statement evaluate to true. FlagTest testItem = FlagTest.Flag1 | FlagTest.Flag2; if (testItem ==…
David Basarab
  • 67,994
  • 42
  • 125
  • 155
112
votes
4 answers

Perl flags -pe, -pi, -p, -w, -d, -i, -t?

I have seen lots of ways of running Perl code or scripts, with different flags. However, when I try to google for what each flag means, I mainly get results to generic Perl sites and no specific information regarding the flags or their use is found…
Tudor Constantin
  • 24,065
  • 7
  • 44
  • 66
101
votes
2 answers

Print All JVM Flags

Found an interesting JVM Flag : java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version It prints hundreds of various options, I never heard about before. It also prints default values, that helps diagnose JVM behaviors better. Another…
Sachin Bhansali
  • 1,212
  • 2
  • 9
  • 12
91
votes
2 answers

GCC: how is march different from mtune?

I tried to scrub the GCC man page for this, but still don't get it, really. What's the difference between -march and -mtune ? When does one use just -march, vs. both? Is it ever possible to just -mtune?
Jameson
  • 5,452
  • 5
  • 26
  • 44
90
votes
12 answers

Anyone know a good workaround for the lack of an enum generic constraint?

What I want to do is something like this: I have enums with combined flagged values. public static class EnumExtension { public static bool IsSet( this T input, T matchTo ) where T:enum //the constraint I want that doesn't exist in…
Keith
  • 133,927
  • 68
  • 273
  • 391
76
votes
3 answers

Start new Activity and finish current one in Android?

Currently I'm starting a new Activity and calling finish on a current one. Is there any flag that can be passed to Intent that enables finishing current Activity without a need to call finish manually from code?
pixel
  • 21,352
  • 30
  • 113
  • 196
72
votes
4 answers

Disable keep screen on

I used: getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); How do I resume to Default state (no-keep-on)?
OkyDokyman
  • 3,298
  • 7
  • 32
  • 48
67
votes
3 answers

Enum flags over 2^32

I am using Enum flags in my application. The Enum can have around 50+ values, so values go up to 2^50. I was just wondering, can I use Math.Pow(2, variable) to calculate these? When I try to do that I get a constant value compile-time error. Is…
Charu
  • 2,439
  • 5
  • 30
  • 51
1
2 3
88 89