Post: [C#] Using enum flags | 'multiple enums in a single argument'
10-16-2015, 12:33 PM #1
Winter
Purple God
(adsbygoogle = window.adsbygoogle || []).push({}); So, if you're like me and you have a literal fetish for structures, enums, classes and all of the kind. You probably already know about this. And for all those smart asses out there that are going to say "why not just use ee.HasFlag", I do this method because it's better to learn things that can be transferred to other languages, like C++. (obviously in C++ you don't need to have the != 0 because it registers 1 and 0 as valid boolean operators)

    
enum exampleEnum { //it's just easier to use bitshifting to do this, then you don't need to use our brains because we're lazy fuckers
flag_0 = (1 << 0),
flag_1 = (1 << 1),
flag_2 = (1 << 2)
}

void exampleFunction(exampleEnum ee) {
if ((ee & exampleEnum.flag_0) != 0)
Console.WriteLine("ee contains 'flag_0'");
if ((ee & exampleEnum.flag_1) != 0)
Console.WriteLine("ee contains 'flag_1'");
if ((ee & exampleEnum.flag_2) != 0)
Console.WriteLine("ee contains 'flag_2'");
}
yes I know you could a handler so you don't need to add a if for each new flag you add

'exampleFunction(exampleEnum.flag_0 | exampleEnum.flag_2)' yields this output;

    
ee contains 'flag_0'
ee contains 'flag_2'


I know this isn't anything special but someone asked me how to use "multiple enums in a single argument" so I thought I might as well make a tut :p

The following 2 users say thank you to Winter for this useful post:

Kryptus, ResistTheSun
10-16-2015, 12:34 PM #2
Alt
Banned
"if you're like me and you have a literal fetish for structures, enums, classes and all of the kind"

Nope.avi
10-16-2015, 12:35 PM #3
Winter
Purple God
Originally posted by Alt View Post
"if you're like me and you have a literal fetish for structures, enums, classes and all of the kind"

Nope.avi


bush has a fat head bush has a fat head bush has a fat head
10-16-2015, 08:24 PM #4
It's not the fact that shifting bits is "easier", its the whole backbone of the bitflag system. I don't know why you didn't just write a tutorial on how bits work along with bitwise operators, but meh. Also, you can do the same math alg (just in reverse order) to find the main exponent - which could be used to access a string array with each enumeration member label, example:
    
int findFlagExp(int flag)
{
// note: after the flag value exceeds 4, you'll have to subtract each count e.g. 8 >> 1 == 4, 4-1 == 3
return flag >> 1;
}

char*enumNames[3] = { "active", "fire", "flood" };

typedef enum : int
{
E_ACTIVE,E_FIRE,E_FLOOD
}example_t;

bool flag_exist(int flag, int value)
{
return (flag & ~value) != flag;
}

inline void yek(example_t yee)
{
if(!flag_exist(yee, example_t::E_ACTIVE)) { printf("Error! flag must contain active!\n"); return;}
printf("flag: %s\n", enumNames[findFlagExp(yee & ~example_t::E_ACTIVE)]);
// check for flag container and do your magic
}
Last edited by Bitwise ; 10-16-2015 at 08:27 PM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo