the use of individual bits in a byte (or a set of bytes) to represent boolean values.
Questions tagged [bitflags]
119 questions
30
votes
6 answers
What Does the [Flags] Attribute Really Do?
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…

user541686
- 205,094
- 128
- 528
- 886
25
votes
1 answer
C# int to Flag Enum
Possible Duplicate:
C# int to enum conversion
Is it somehow possible to convert an int to a flag combination enum? So, if
[Flags]
public enum Foo {a = 0x80,
b = 0x40,
c = ...,
...
…

john
- 4,043
- 7
- 27
- 39
23
votes
3 answers
Type safe(r) bitflags in C++?
While revising some old c++ code, I ran across several bitflags defined as enums.
enum FooFlags
{
FooFlag1 = 1 << 0,
FooFlag2 = 1 << 1,
FooFlag3 = 1 << 2
// etc...
};
This isn't uncommon, but it bothered me that as soon as you start…

luke
- 36,103
- 8
- 58
- 81
17
votes
4 answers
How should I represent a bit flags int field in django admin?
I have a data model with a bitfield defined something like this:
alter table MemberFlags add column title varchar(50) not null default '';
alter table MemberFlags add column value integer( 3) not null default 0;
insert into MemberFlags (title,…

Dagg Nabbit
- 75,346
- 19
- 113
- 141
14
votes
3 answers
Are enums the canonical way to implement bit flags?
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
…

Yohaï-Eliel Berreby
- 1,165
- 3
- 14
- 25
13
votes
4 answers
Iterate through values in @IntDef, @StringDef or any @Def class
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;
…

Chad Bingham
- 32,650
- 19
- 86
- 115
10
votes
6 answers
Actual uses of bit flags in .NET framework
Was looking at how enums can be used as bit flags by decorating them with the flags attribute and bitwize operators (see below).
Are there any places in the .NET framework that this pattern is used? I like this but want to see some more real life…

AJM
- 32,054
- 48
- 155
- 243
10
votes
2 answers
How can I define an enumeration where multiple values map to a single label?
Suppose, for the sake of this example, that I am trying to parse a file which specifies that two arbitrary bytes in the record represent the day of the week, thusly:
DayOfWeek:
- 0 = Monday
- 1 = Tuesday
- 2 = Wednesday
- 3 =…

GWLlosa
- 23,995
- 17
- 79
- 116
9
votes
2 answers
Simple boolean operators for bit flags
I am attempting to learn more about this to implement in my project.
I currently have got this basically:
unsigned char flags = 0; //8 bits
flags |= 0x2; //apply random flag
if(flags & 0x2) {
printf("Opt 2 set");
}
Now I am wishing to do a…

John
- 1,110
- 3
- 14
- 28
8
votes
6 answers
Why does [Flag]'d enums start at 0 and increment by 1?
Edit: It seems most people misunderstood my question.
I know how enum works, and I know binary. I'm wondering why the enums with the [Flags] attribute is designed the way it is.
Original post:
This might be a duplicate, but I didn't find any other…

simendsjo
- 4,739
- 2
- 25
- 53
8
votes
8 answers
Determine which single bit in the byte is set
I have a byte I'm using for bitflags. I know that one and only one bit in the byte is set at any give time.
Ex: unsigned char b = 0x20; //(00100000) 6th most bit set
I currently use the following loop to determine which bit is set:
int…

recursion.ninja
- 5,377
- 7
- 46
- 78
7
votes
2 answers
Flags in VB6 does not return a correct value
I am currently trying to use a bit flag enum in a VB6 / COM project.
However, when trying to read values from the enum, I get inconsistent results.
Here is the enum definition :
Enum Fruits
None = 0
Apple = 1
Strawberry = 2
Lemon =…

Thibault Falise
- 5,795
- 2
- 29
- 32
7
votes
1 answer
How to match against a flags/bitmasks in Rust?
Take a typical check for bit-flags:
if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
How would this be written as a match statement?

ideasman42
- 42,413
- 44
- 197
- 320
6
votes
1 answer
Rust bitfields and enumerations C++ style
I'm a Rust beginner which comes from C/C++. To start off I tried to create a simple "Hello-World" like program for Microsoft Windows using user32.MessageBox where I stumbled upon a problem related to bitfields. Disclaimer: All code snippets were…

Maurice Kayser
- 455
- 4
- 11
6
votes
2 answers
Is this the most optimal way? C bitfields
I made a function to set or clear a specific number of bits in a DWORD. My function works. I don't need help making it work. However, I am wondering if the method I've chosen to do it is the fastest possible way.
It's rather hard for me to explain…

oldSkool
- 1,212
- 5
- 14
- 29