5

Compiles but shouldn't

[Flags]
enum TransactionData : long  // 64 bits.  Last bit is sign bit, but I'm putting data there
{
    None = 0,
    Color1 = 1 << 63,
}

Errors but shouldn't

[Flags]
enum TransactionData : ulong  // 64 bits. No sign bit.  Not allowed to put data there
{
    None = 0,
    Color1 = 1 << 63,
}

Compiler Error Text:

-2147483648 cannot be converted to a ulong

Question:

I would expect the opposite to occur. Can anyone explain why this is?

Also how I can print this flags attribute to a byte[] for inspection?

 var eee  = TransactionData.None | TransactionData.Color1
 // How do I convert eee to byte[]?
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

14

Note that 1 << 63 isn't a ulong or even a long. The compiler interprets it as an int. Observe the following example:

enum TransactionData : long
{
    None = 0,
    Color1 = 1 << 31,
    Color2 = 1 << 63,
}

Console.WriteLine(TransactionData.Color1 == TransactionData.Color2); // True

However, you can coerce the compiler into interpreting it as a ulong by adding ul to the end:

enum TransactionData : ulong
{
    None = 0,
    Color1 = 1ul << 63,
}

Although many people prefer using an upper case L because the lowercase l looks a lot like a number 1. A full list of what suffixes are supported by the compiler can be found here.

Also, I should point out that 1ul << 63 is actually 64 bits wide (it's one bit, shifted by 63 bits).

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Funny thing, I've been working with C# for 8 years, and just learned the concept of appending letters to cast a number to a different types. Wonder how many people are in the same boat as me. – makerofthings7 Mar 12 '14 at 22:57
  • Do you know how I can cast this ULong enum to a Byte[] perchance? (ps will accept yours no matter what in 6 min.. when the timeout expires. – makerofthings7 Mar 12 '14 at 22:58
  • @makerofthings7 See the [BitConverter](http://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes(v=vs.110).aspx) class, e.g. `BitConverter.GetBytes((ulong)TransactionData.Color1)` – p.s.w.g Mar 12 '14 at 23:02