long 类型的最大十六进制值
我已将 Java 代码移植到 C#。 您能否解释一下为什么我在下面的行中出现编译时错误(我使用 VS 2008):
private long l = 0xffffffffffffffffL; // 16 'f' got here
无法将源类型 ulong 转换为目标类型 long
我在这里需要与原始 Java 代码相同的值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 不介意常量在这种特定情况下是否溢出 - 您给出的值实际上是 -1。
在 C# 中实现相同效果的最简单方法是:
如果您想保留 16 个 fs,您可以使用:
如果您实际上想要有符号 long 的最大值,您应该使用:
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:
If you want to retain the 16 fs you could use:
If you actually want the maximum value for a signed long, you should use:
假设您不担心负值,您可以尝试使用
unsigned long
:在 Java 中,
l
的实际值是-1
,因为它会溢出2^63 - 1
最大值,因此您可以将常量替换为-1
。Assuming you aren't worried about negative values, you could try using an
unsigned long
:In Java the actual value of
l
would be-1
, because it would overflow the2^63 - 1
maximum value, so you could just replace your constant with-1
.0xffffffffffffffff
大于有符号长整型所能表示的大小。您可以插入强制转换:
由于 C# 使用二进制补码,
0xffffffffffffffff
表示-1
:或者将变量声明为无符号,如果您想表示位,这可能是最干净的选择模式:
singed long 的最大值为:
但最好写成
long.MaxValue
。0xffffffffffffffff
is larger than a signed long can represent.You can insert a cast:
Since C# uses two's complement,
0xffffffffffffffff
represents-1
:Or declare the variable as unsigned, which is probably the cleanest choice if you want to represent bit patterns:
The maximal value of a singed long is:
But that's better written as
long.MaxValue
.你可以这样做:
...但正如 mdm 指出的,你可能实际上想要一个 ulong。
You could do this:
... but as mdm pointed out, you probably actually want a ulong.