理解 Java 无符号数
我想了解如何将有符号数转换为无符号数。
可以说我有这个:
byte number = 127; // '1111111'
为了使其无符号,我必须选择“更大”的数据类型“short”并应用值为 0x00ff 的 AND 运算符。
short number2;
number2 = number & 0x00ff;
为什么它使数字无符号?
I want to understand how to convert signed number into unsigned.
lets say I have this:
byte number = 127; // '1111111'
In order to make it unsigned I have to choose "bigger" data type 'short' and apply AND operator with value 0x00ff.
short number2;
number2 = number & 0x00ff;
Why does it make the number unsigned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 实际上没有无符号原语。
值 127 实际上由“01111111”表示,第一位是符号(0 是正数)。
无符号字节可以保存 0 到 255 之间的值,但有符号字节的最大值为 127。由于一个字节有 8 位,有符号的位需要消耗 1 位来保存符号。
因此,如果要表示大于 127 的值,则需要使用具有更多位数的更大类型。更大的类型还有一个保留的符号位,但它至少有 8 位用于实际值,因此您可以表示值 255。
话虽如此,您可能应该避免使用 byte 和 Short,因为它们存在问题。您会注意到我将结果转换为short,因为运算符实际上返回int。你应该只使用 java 中的 int 和 long,因为它们实现得更好。
编辑:AND 运算符使其无符号,因为符号位是短整型的第一位,并且您将保存字节值的 8 位复制到短整型的最后 8 位。因此,如果你有一个负数,第一位 1(这意味着它是负数)实际上成为该值的一部分。并且短路将始终为正,因为其符号位处于受短路影响的 2 的幂的两个高位。
Java doesn't actually have unsigned primitives.
The value 127 is actually represented by '01111111' the first bit being the sign (0 is positive).
An unsigned byte would be able to hold values 0 to 255, but 127 is the maximum for a signed byte. Since a byte has 8 bits, and the signed one consumes one to hold the sign.
So if you want to represent values larger than 127 you need to use a bigger type that has a greater number of bits. The greater type also has a reserved bit for sign, but it has at least 8 bits used for the actual values, so you can represent the value 255.
That being said, you should probably avoid using byte and short because there are issues with them. You'll notice i cast the result to short, since the operators actually return int. You should just stick to int and long in java since they are implemented better.
Edit: the AND operator makes it unsigned since the sign bit is the first bit of the short, and you copy the 8 bits holding the value of the byte to the last 8 bits of the short. So if you have a negative number the first bit which is 1 (that means it's negative) actually becomes part of the value. And the short will always be positive since its sign bit is at two high of a power of two to be affected by the short.
||||| <- actual value
编辑 2:请注意,由于负值使用 二进制补码表示,因此该值可能不会成为您所期望的吧。所有正值保持不变。
但是-128 = 0x10000000 会变成128
-127 = 0x10000001 将变为 129
依此类推,直到-1 = 0x11111111,这将变成255
||||| <- actual value
Edit 2: Take note though that since the negative values use two's complement representation the value might not be what you expect it. all the positive values remain the same.
But -128 = 0x10000000 will become 128
-127 = 0x10000001 will become 129
and so on until -1 = 0x11111111 which will become 255
java没有无符号数据类型。您只需切换到“更大”的数据类型即可。
java does not have unsigned data types. You just make switch to "bigger" data type, that's all.
如果你只是从负字节移动到 int,那么 int 也将是负数。因此,您采用负 int(或示例中的 Short)并将最高 3 个字节(int)设置为 0x00。
If you just move from a negative byte to int the int will be negative too. Sou you take the negative int (or short in your example) and set the highest 3 byte (int) to 0x00.