long 类型的最大十六进制值

发布于 2024-12-26 01:03:26 字数 226 浏览 2 评论 0原文

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声情话 2025-01-02 01:03:26

Java 不介意常量在这种特定情况下是否溢出 - 您给出的值实际上是 -1。

在 C# 中实现相同效果的最简单方法是:

private long l = -1;

如果您想保留 16 个 fs,您可以使用:

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

如果您实际上想要有符号 long 的最大值,您应该使用:

// Java
private long l = Long.MAX_VALUE;
// C#
private long l = long.MaxValue;

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;
独享拥抱 2025-01-02 01:03:26

假设您不担心负值,您可以尝试使用 unsigned long

private ulong l = 0xffffffffffffffffL;

在 Java 中,l 的实际值是 -1,因为它会溢出2^63 - 1 最大值,因此您可以将常量替换为 -1

Assuming you aren't worried about negative values, you could try using an unsigned long:

private ulong l = 0xffffffffffffffffL;

In Java the actual value of l would be -1, because it would overflow the 2^63 - 1 maximum value, so you could just replace your constant with -1.

小梨窩很甜 2025-01-02 01:03:26

0xffffffffffffffff 大于有符号长整型所能表示的大小。

您可以插入强制转换:

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

由于 C# 使用二进制补码,0xffffffffffffffff 表示 -1

private long l = -1;

或者将变量声明为无符号,如果您想表示位,这可能是最干净的选择模式:

private ulong l = 0xffffffffffffffffL;
private ulong l = ulong.MaxValue;

singed long 的最大值为:

private long l = 0x7fffffffffffffffL;

但最好写成 long.MaxValue

0xffffffffffffffff is larger than a signed long can represent.

You can insert a cast:

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

Since C# uses two's complement, 0xffffffffffffffff represents -1:

private long l = -1;

Or declare the variable as unsigned, which is probably the cleanest choice if you want to represent bit patterns:

private ulong l = 0xffffffffffffffffL;
private ulong l = ulong.MaxValue;

The maximal value of a singed long is:

private long l = 0x7fffffffffffffffL;

But that's better written as long.MaxValue.

风向决定发型 2025-01-02 01:03:26

你可以这样做:

private long l = long.MaxValue;

...但正如 mdm 指出的,你可能实际上想要一个 ulong。

private ulong l = ulong.MaxValue;

You could do this:

private long l = long.MaxValue;

... but as mdm pointed out, you probably actually want a ulong.

private ulong l = ulong.MaxValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文