c++ 中的 unsigned long long 问题
我有一个 cpp 文件,名为 xyz.cpp
,其中包含长常量。现在我需要将 long 常量更改为 long long。
ex
long a=0x00000001
为了
long long a=0x0000000000000001
未来的目的。 (我使用gcc编译器) 但是当我这样做时,我收到“整数值太大而无法保存长值”错误。 当通过互联网浏览时,我收到了诸如使用之类的建议,
long long a=0x0000000000000001ULL .
效果很好。但问题是我有一个 jar 文件,需要将此 .cpp
文件转换为 .java
。当它尝试从 .cpp
文件转换 .java
文件时,它无法识别 ULL。
现在我的问题是
1,对于这个场景,我的 gcc 编译器是否接受 long long 值,而不是在末尾添加 ULL 2,或者建议我应该在.java文件中做什么来接受那个long long值(ULL)(我知道java只有long值可以保存long long值)
提前感谢:)
i have a cpp file say xyz.cpp
, which contains long constants. now i need to change long constants to long long.
ex
long a=0x00000001
to
long long a=0x0000000000000001
for future purpose. ( i use gcc compiler )
But when i do so, i got "integer value is to large to hold the long value" error.
when browsed over internet, i got a suggestion like use,
long long a=0x0000000000000001ULL .
that worked fine. but the problem is i ve a jar file, that need to convert this .cpp
file to .java
. when it try to convert a .java
file from .cpp
file, it does not recognizes ULL.
now my question is
1, to this scenerio, is this anyway for my gcc compiler to make accept long long values, instead of adding ULL @ the end
2, or suggest me what should i do in .java file to accept that long long value (ULL) ( i know java has only long value that can hold long long value )
thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 C++ 在不修改源代码的情况下不会编译为 java,因此您可以只删除 ULL/LL 后缀(并将
long long
更改为long
)。您只需将其添加到转换时要更改的内容列表中 - 我没有看到问题?Since C++ won't compile as java without modifying the source, you could just strip the ULL/LL suffix (and change
long long
tolong
). You'll simply need to add this to the list of things to change when converting - I don't see the problem?那么,您究竟想做什么,将 C++ 代码转换为 Java 代码?
Java没有无符号整数类型,C++中的“long long”类型在Java中也不存在。 Java 具有以下整数类型:
byte
- 8 位有符号整数short
- 16 位有符号整数int
- 32 位有符号整数< code>long - 64 位有符号整数
(还有
char
,从技术上讲,它是 16 位无符号整数,但用于保存字符数据)。如果您需要处理不适合
long
的数字,您可以在 Java 中使用BigInteger
。So, what exactly are you trying to do, convert C++ code to Java?
Java does not have unsigned integer types, and the "long long" type from C++ also does not exist in Java. Java has the following integer types:
byte
- 8-bit signed integershort
- 16-bit signed integerint
- 32-bit signed integerlong
- 64-bit signed integer(There is also
char
, which is technically a 16-bit unsigned integer, but which is meant to hold character data).You could use
BigInteger
in Java if you need to work with numbers that do not fit in along
.long
在 Java 中可以保存 64 位,并且在重要的地方具有签名行为。然而,这并不能阻止您在其中存储无符号 64 位值。您需要为某些操作编写变通方法,但 +、-、*、==、!= 等的工作原理完全相同。long
can hold 64-bits in Java, with signed behaviour where it matters. However this doesn't stop you storing unsigned 64-bit values in it. You need to write work arounds for certain operations, but +, -, *, == , != etc all work exactly the same.