How to Compare Flags in C#?

In .NET 4 there is a new method Enum.HasFlag. This allows you to write: if ( testItem.HasFlag( FlagTest.Flag1 ) ) { // Do Stuff } which is much more readable, IMO. The .NET source indicates that this performs the same logic as the accepted answer: public Boolean HasFlag(Enum flag) { if (!this.GetType().IsEquivalentTo(flag.GetType())) { throw new … Read more

Why do enum permissions often have 0, 1, 2, 4 values?

Because they are powers of two and I can do this: var permissions = Permissions.Read | Permissions.Write; And perhaps later… if( (permissions & Permissions.Write) == Permissions.Write ) { // we have write access } It is a bit field, where each set bit corresponds to some permission (or whatever the enumerated value logically corresponds to). … Read more

Bash if statement with multiple conditions throws an error

Use -a (for and) and -o (for or) operations. tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html Update Actually you could still use && and || with the -eq operation. So your script would be like this: my_error_flag=1 my_error_flag_o=1 if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 … Read more

Most common C# bitwise operations on enums

I did some more work on these extensions – You can find the code here I wrote some extension methods that extend System.Enum that I use often… I’m not claiming that they are bulletproof, but they have helped… Comments removed… namespace Enum.Extensions { public static class EnumerationExtensions { public static bool Has<T>(this System.Enum type, T … Read more

Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable. Assuming you are invoking g++ from the command line (terminal): $ g++ -std=c++11 your_file.cpp -o your_program or $ g++ -std=c++0x your_file.cpp -o your_program if the above doesn’t work.

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

The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value. Such collections are often used with bitwise operators, for example: var allowedColors = MyColor.Red | MyColor.Green | MyColor.Blue; Note that the [Flags] attribute doesn’t enable this by itself – all it does is allow a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)