java 无符号 16 位和 64 位整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7306825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unsigned 16 and 64 bit integers
提问by Roger F. Gay
How do you produce a 16 bit unsigned integer and a 64-bit unsigned integer in Java? This question is related to implementing a standard. I am not in a position to change the spec.
你如何在 Java 中生成一个 16 位无符号整数和一个 64 位无符号整数?这个问题与实施标准有关。我无法更改规范。
Other perhaps relevant bits of the spec. this question is related to:
规范的其他可能相关的部分。这个问题与:
- most significant bit MUST be 0.
- must be expressed in network byte order.
- 最高有效位必须为 0。
- 必须以网络字节顺序表示。
Application context: The number (in either form) represents the length of data being sent. The length can be big or small. I've first found the length of the message to be sent as a long.
应用程序上下文:数字(以任何一种形式)表示正在发送的数据的长度。长度可大可小。我首先发现要发送的消息的长度很长。
So I'm starting with: long length = getLength();
所以我开始: long length = getLength();
I then need to convert the long variable "length" to either of the two above, depending on how big it is. In the end, I'm pretty sure I'll need to do a .getBytes()
when I send the length. The recipient will interpret as described above.
然后我需要将 long 变量“length”转换为上述两个中的任何一个,具体取决于它有多大。最后,我很确定.getBytes()
在发送长度时我需要做一个。接收者将按上述方式进行解释。
回答by Stephen C
If the most significant bit must be zero, then the number is the same whether it is signed or unsigned (assuming a two's complement representation). So for instance, 16 bit integers with the MSB zero represent the numbers from 0
to 32767
inclusive.
如果最高有效位必须为零,那么无论是有符号还是无符号数字都是相同的(假设是二进制补码表示)。因此,例如,带有 MSB 零的 16 位整数表示从0
到32767
包含的数字。
Assuming that you are writing to an OutputStream
and that your definition of "network order" is most significant byte first, then you need to do something like this:
假设您正在写入 anOutputStream
并且您对“网络顺序”的定义是最重要的字节,那么您需要执行以下操作:
public void writeShort(OutputStream os, short s) throws IOException {
os.write((byte) (s >> 8));
os.write((byte) s);
}
and
和
public void writeLong(OutputStream os, long l) throws IOException {
os.write((byte) (l >> 56));
os.write((byte) (l >> 48));
os.write((byte) (l >> 40));
os.write((byte) (l >> 32));
os.write((byte) (l >> 24));
os.write((byte) (l >> 16));
os.write((byte) (l >> 8));
os.write((byte) l);
}
Note that these works for signed and unsigned integers. (Or to be more precise for the Java context, they work if the argument representsa signed or unsigned integer.)
请注意,这些适用于有符号和无符号整数。(或者更准确地说,对于 Java 上下文,如果参数表示有符号或无符号整数,它们就可以工作。)
回答by leifg
Java has no unsigned datatypes (see here: http://darksleep.com/player/JavaAndUnsignedTypes.html)
Java 没有未签名的数据类型(请参阅此处:http: //darksleep.com/player/JavaAndUnsignedTypes.html)
int has 32bit, long has 64bit, short has 16bit
int 有 32bit,long 有 64bit,short 有 16bit
is it so bad to store it all just as long?
把它全部保存那么久有那么糟糕吗?