Quick bit field question
-
Hi, how do I extract values from a bit field in c#? I tried it a la C++ if(flag.value & enum.value) { } but it wouldn't let me go that way, thought it was boolean or something odd. Anyway, I've had a quick look thru previous posts (takes a bloody long time to load pages!) but forgive me if it's been covered. Thanks, Brian. "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.
-
Hi, how do I extract values from a bit field in c#? I tried it a la C++ if(flag.value & enum.value) { } but it wouldn't let me go that way, thought it was boolean or something odd. Anyway, I've had a quick look thru previous posts (takes a bloody long time to load pages!) but forgive me if it's been covered. Thanks, Brian. "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.
It's the other way round: the
if
statement requires the condition to evaluate to typebool
in C#. You can't simply test an integer. Ifenum.value
represents a single flag, useif( ( flag.value & enum.value ) == enum.value )
{
}If it represents a mask of flags, and you want to know if any of the masked flags are set, use
if( ( flag.value & enum.value ) != 0 )
{
}You might need to add the
FlagsAttribute
to yourenum
for the latter to work. Stability. What an interesting concept. -- Chris Maunder -
It's the other way round: the
if
statement requires the condition to evaluate to typebool
in C#. You can't simply test an integer. Ifenum.value
represents a single flag, useif( ( flag.value & enum.value ) == enum.value )
{
}If it represents a mask of flags, and you want to know if any of the masked flags are set, use
if( ( flag.value & enum.value ) != 0 )
{
}You might need to add the
FlagsAttribute
to yourenum
for the latter to work. Stability. What an interesting concept. -- Chris MaunderCool, thanks heaps! "Ergo huffabo et puffabo et tuam domum inflabo" ait magnus malus lupus. "Non per comam men-men-menti!" ait porcellus.