Why are flag enums usually defined with hexadecimal values

Rationales may differ, but an advantage I see is that hexadecimal reminds you: “Okay, we’re not dealing with numbers in the arbitrary human-invented world of base ten anymore. We’re dealing with bits – the machine’s world – and we’re gonna play by its rules.” Hexadecimal is rarely used unless you’re dealing with relatively low-level topics where the memory layout of data matters. Using it hints at the fact that that’s the situation we’re in now.

Also, i’m not sure about C#, but I know that in C x << y is a valid compile-time constant.
Using bit shifts seems the most clear:

[Flags]
public enum MyEnum
{
    None  = 0,
    Flag1 = 1 << 0,  //1
    Flag2 = 1 << 1,  //2
    Flag3 = 1 << 2,  //4
    Flag4 = 1 << 3,  //8
    Flag5 = 1 << 4   //16
}

Leave a Comment

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