Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max HEX value for long type

I have ported Java code to C#. Could you please explain why I have compile-time error in the follow line (I use VS 2008):

    private long l = 0xffffffffffffffffL; // 16 'f' got here

Cannot convert source type ulong to target type long

I need the same value here as for origin Java code.

like image 851
Michael Z Avatar asked Mar 01 '26 17:03

Michael Z


1 Answers

Java doesn't mind if a constant overflows in this particular situation - the value you've given is actually -1.

The simplest way of achieving the same effect in C# is:

private long l = -1;

If you want to retain the 16 fs you could use:

private long l = unchecked((long) 0xffffffffffffffffUL);

If you actually want the maximum value for a signed long, you should use:

// Java
private long l = Long.MAX_VALUE;
// C#
private long l = long.MaxValue;
like image 90
Jon Skeet Avatar answered Mar 03 '26 07:03

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!