-1

I have found this enum with a Flags attribute:

    [Flags]
    public enum Technology
    {
       None = 0x0000,
        X1 = 0x0001,
        X2 = 0x0002,
        X3 = 0x0004,
        X4 = 0x0008,
        X5 = 0x0010,
        X6 = 0x0020,       
    }

The values assigned to the enum members, what spelling is it?

And would that be the same?

    [Flags]
    public enum Technology
    {
       None = 0,
        X1 = 1,
        X2 = 2,
        X3 = 4,
        X4 = 8,
        X5 = 10,
        X6 = 20,       
    }

If yes, why is X5 not 16 twice the value from X4, because that logic takes place from X1 to X4!

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
HelloWorld
  • 4,671
  • 12
  • 46
  • 78
  • 1
    Because 10 is double 8 in hex? – BugFinder Nov 06 '19 at 10:55
  • 1
    `0x0020` is *hexadecimal* (please, note `0x` *prefix*) which is `32` *decimal*; so `X5 = 16, X6 = 32` – Dmitry Bychenko Nov 06 '19 at 10:55
  • 1
    `0x` means the number is hexadecimal – Cid Nov 06 '19 at 10:56
  • 1
    Does this answer your question? [What do numbers using 0x notation mean?](https://stackoverflow.com/questions/8186965/what-do-numbers-using-0x-notation-mean) – Sinatr Nov 06 '19 at 11:20
  • Possible duplicate of [What do numbers using 0x notation mean?](https://stackoverflow.com/questions/8186965/what-do-numbers-using-0x-notation-mean) and [What does the Flags Enum Attribute mean in C#?](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) –  Nov 06 '19 at 11:34

2 Answers2

4

Well, 0x prefix means hexadecimal, not just decimal. So the equivalent declarations will be

hexadecimal (0x prefix)

[Flags]
public enum Technology
{
   None = 0x0000,
     X1 = 0x0001,
     X2 = 0x0002,
     X3 = 0x0004,
     X4 = 0x0008,
     X5 = 0x0010, // 0x0010 == 1 * 16 + 0 == 16
     X6 = 0x0020, // 0x0020 == 2 * 16 + 0 == 32       
}

decimal

[Flags]
public enum Technology
{
   None =  0,
     X1 =  1,
     X2 =  2,
     X3 =  4,
     X4 =  8,
     X5 = 16,
     X6 = 32,       
}

binary (note 0b prefix)

[Flags]
public enum Technology
{
   None = 0b000000,
     X1 = 0b000001,
     X2 = 0b000010,
     X3 = 0b000100,
     X4 = 0b001000,
     X5 = 0b010000,
     X6 = 0b100000,       
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
3

0x0000 is 0 in Hexadecimal. Which means that 0x0010 is actually 16 in decimal - making the x5 and x6 members of both enums different.

In order to make them the same, in the second enum you need the values 16 and 32 for x5 and x6.

The 0x prefix is what tells the c# compiler that the value is hexadecimal - as documented in the Integer Literals paragraph in Integral numeric types (C# reference):

Integer literals can be

  • decimal: without any prefix
  • hexadecimal: with the 0x or 0X prefix
  • binary: with the 0b or 0B prefix (available in C# 7.0 and later)
Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121