Question

I'm trying to write Int64.MinValue (-0x8000000000000000) as a negative hexadecimal, but Visual Studio 2010 doesn't allow me because it assumes that 0x8000000000000000 must be a ulong literal, and and unsigned number cannot be negative. Is there a workaround for this? I noticed the use of the checked keyword in another thread, but it didn't solve this issue. I'd really like to use hex notation over decimal in this situation.

Interestingly, this doesn't happen with a decimal notation.

Was it helpful?

Solution

This shows a few possible ways to achieve it:

static void Main(string[] args)
{
    Int64 minValue = -9223372036854775808;
    Int64 minValueViaHex = Int64.Parse("8000000000000000", NumberStyles.AllowHexSpecifier);
    Int64 minValueViaHexInline = unchecked((long)0x8000000000000000L);

    Console.WriteLine("Decimal match : {0}", Int64.MinValue == minValue);
    Console.WriteLine("Via Hex match : {0}", Int64.MinValue == minValueViaHex);
    Console.WriteLine("Via Hex inline: {0}", Int64.MinValue == minValueViaHexInline);
}

This outputs:

Decimal match : True
Via Hex match : True
Via Hex inline: True

OTHER TIPS

How about this?

const Int64 test = unchecked((long)0x8000000000000000);
Trace.Assert(test == Int64.MinValue);

Or is that not what you meant?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top