473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Enumartion flags

Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.
Nov 15 '05 #1
10 4161
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.

Nov 15 '05 #2
100
Hi Jose,

[Flags]
enum MyFlags
{
First = 0x01,
Second = 0x02,
Third = 0x04
}

[Flags] attribute is not necessary but it changes the way ToString() method
works.

HTH
B\rgds
100

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.

Nov 15 '05 #3
100
Hi Nicholas,

Flags doesn't have to be power of 2.

B\rgds
100

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eQ**************@tk2msftngp13.phx.gbl...
Jose,

If you are going to use flag values, then you have to make sure that the values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to asign the members values (0x01, 0x02) etc...

Thanks.
Jose.


Nov 15 '05 #4
Thanks Nicholas,
Do you know how to make a member represent several of the other
members?

public enum Border{
None = 0x0000,
Top = 0x0001,
Right = 0x0002
Left = 0x0004,
Bottom = 0x0008
All = ?
}

Thanks Again.
Jose.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eQ**************@tk2msftngp13.phx.gbl...
Jose,

If you are going to use flag values, then you have to make sure that the values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to asign the members values (0x01, 0x02) etc...

Thanks.
Jose.


Nov 15 '05 #5
It doesn't have to be, but it should be if you are going to make it work
correctly.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"100" <10*@100.com> wrote in message
news:On*************@TK2MSFTNGP12.phx.gbl...
Hi Nicholas,

Flags doesn't have to be power of 2.

B\rgds
100

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eQ**************@tk2msftngp13.phx.gbl...
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make enumaration flags? Not the structure (public enum MyFlags{...}) but
how to asign the members values (0x01, 0x02) etc...

Thanks.
Jose.



Nov 15 '05 #6
All = None | Top | Right | Left | Bottom

| sets a flag
& tests for it
"Jose Ines" <jo************@msn.com> wrote in message
news:es*************@TK2MSFTNGP10.phx.gbl...
Thanks Nicholas,
Do you know how to make a member represent several of the other
members?

public enum Border{
None = 0x0000,
Top = 0x0001,
Right = 0x0002
Left = 0x0004,
Bottom = 0x0008
All = ?
}

Thanks Again.
Jose.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eQ**************@tk2msftngp13.phx.gbl...
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jose Ines" <jo************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hello,
Can anyone give me a link where I can find a tutorial on how to make enumaration flags? Not the structure (public enum MyFlags{...}) but
how to asign the members values (0x01, 0x02) etc...

Thanks.
Jose.



Nov 15 '05 #7
100
Hi,
It doesn't have to be, but it should be if you are going to make it work correctly.


What do you mean?
I may have flag that uses more then one bit.

for example one flag occupies the first two less significant bits (0 and
1st) (flag1 = 0x3)
then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

enum MyFlags
{
Flag1 = 0x3,
Flag2 = 0x14
}
MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

Do you want to say that this is incorrect or this won't work correctly?
Yes, I agree that usually one flag uses only one bit, but not always. There
is nothing wrong to have one flag represented by more then one bit. It will
work correctly as long as the semantic of those flags is correct.

B\rgds
100
Nov 15 '05 #8
Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to
the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"100" <10*@100.com> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi,
It doesn't have to be, but it should be if you are going to make it work
correctly.


What do you mean?
I may have flag that uses more then one bit.

for example one flag occupies the first two less significant bits (0 and
1st) (flag1 = 0x3)
then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

enum MyFlags
{
Flag1 = 0x3,
Flag2 = 0x14
}
MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

Do you want to say that this is incorrect or this won't work correctly?
Yes, I agree that usually one flag uses only one bit, but not always.

There is nothing wrong to have one flag represented by more then one bit. It will work correctly as long as the semantic of those flags is correct.

B\rgds
100

Nov 15 '05 #9
100
When it goes for flags we are limited anyway by the underlying type of the
enumerations.
The ony reason for bringing this is because from the posts releated to
using enumerations as flags, I've seen, I got the impression that a lot of
programmers think that if they specify [Flags] attributes the values for the
enum's items *have to be* power of 2 otherwise they won't work.

B\rgds
100

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ex**************@TK2MSFTNGP12.phx.gbl...
Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"100" <10*@100.com> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi,
It doesn't have to be, but it should be if you are going to make
it work
correctly.


What do you mean?
I may have flag that uses more then one bit.

for example one flag occupies the first two less significant bits (0 and
1st) (flag1 = 0x3)
then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

enum MyFlags
{
Flag1 = 0x3,
Flag2 = 0x14
}
MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

Do you want to say that this is incorrect or this won't work correctly?
Yes, I agree that usually one flag uses only one bit, but not always.

There
is nothing wrong to have one flag represented by more then one bit. It

will
work correctly as long as the semantic of those flags is correct.

B\rgds
100


Nov 15 '05 #10
> Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to
the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).

Actually, you are limited to the type of your enum. By default, being an int,
that would be 32, but you can also just as easily define the following if you do
not need to spend 4 bytes of space. But then again, what is space. :)

public enum MyFlags : short
{
None = 0,
First = 1,
Second = 2,
Third = 4
}
.... and so on. In this case, the maximum effective bit mappings would be 16.

But then, I know that you knew this. I figured I would point out that the type
of an enum could be declared as well. I did not know this until last night.

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Xah Lee | last post by:
Python re module has methods flags and pattern. How to use these exactly? e.g. i tried print patternObj.flags() and the error is some "int object is not callable". ...
7
by: loquak | last post by:
Greets. I wonder which way would be the best for holding some state flags in terms of memory use and efficiency. a) struct FLAGS { bool flag1 : 1; bool flag2 : 1;
6
by: Alexis Gatt | last post by:
Hi guys, I was reading the source code of a lib, and I came across this odd way of defining flags #define PHONG_RV (0<<0) #define PHONG_NH (1<<0) Here is how they're used:
13
by: Mark A. Odell | last post by:
I write a lot of drivers and there is inevitably some hardware register or buffer descriptor field called 'flags'. The flags are defined, typically, as bit positions. I was thinking I could get...
3
by: Ignacio X. Domínguez | last post by:
Hi. I'm using the | operator to use multiple flags in a variable. For example: const int flag1 = 0x00000001; const int flag2 = 0x00000002; const int flag3 = 0x00000004; int Flags = flag2 |...
1
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte,...
1
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an...
1
by: RicercatoreSbadato | last post by:
I have notice that my images have the value Image.Flags = 2 (HasAlpha). How Can I set the Flags to 1? I would like to eliminate the alpha channel... -- RicercatoreSbadato
1
by: Alexander Korsunsky | last post by:
Hi! Is it possible to extract the mode flags of a stream (FILE* stream or fstream - object), without looking at how the stream was opened? What I mean by mode flags is wether the stream is opened...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.