在 Java 中将 long 转换为 byte
我无法理解以下内容:
在java中,
long l = 130L;
byte b = (byte)l;
如果我打印b的值,为什么会得到-126? long l 的位表示是什么?
I am unable to understand the following:
In java,
long l = 130L;
byte b = (byte)l;
If I print the value of b, why do I get -126? What is the bit representation of long l?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个字节是8位的序列,这使得2^8种情况=256。其中一半表示负数,即-128到-1。然后是0,大约一半,1到127代表正数。
Int 形式的 130 看起来像 128 + 2,即:
但是,Byte 只有 8 位,赋值时只按原样取位,但只取最后一位:
第一个位表示,它是一个负数。现在你需要添加多少才能达到零?让我们逐步进行:
让我们采取更大的步骤。只需在有零的地方加 1,但首先我们回到 x:
现在有转折点:我们再加 1,得到零(加上溢出)
如果 (x+y) + 1 = 0,x+y = -1。有趣的是,负 1 不仅与设置了“负标志”(“1000:0001”) 的 1 (0000:0001) 相同,而且看起来完全不同。但是,第一个位置总是告诉您符号:1 始终表示负数。
但我们之前添加了什么?
它的第一个位置没有 1,因此它是一个正值。我们知道如何解构它吗?
现在很清楚了:x+125 = -1 ==> x = -126
您可以想象这些值排列成一个圆圈,0 位于顶部(正午),正值排列得像时钟上的 0 到 5(但到 127),而转折点位于底部(127 + 1 => -128 [原文如此!]。)现在您可以顺时针方向继续,将 1 导数加到 -127、-126、-125、... -3、-2、-1(在 11 点) 'clock),最后 0 再次到达顶部。
对于更大的数字(小、整数、长),需要更大的时钟,零总是在顶部,最大值和最小值总是在底部。但即使是一个字节也太大了,无法制作图片,所以我制作了一个半字节,半字节:
您可以轻松地填补图片中的漏洞,这很简单!
顺便说一句:整个事情不叫选角。强制转换仅用于对象之间。如果你有一些东西,它实际上是一个子类型:
这只是一个赋值,因为字符串(总是)是一个对象。不涉及铸造。
这是一个铸件。到更具体的类型。并非每个对象都是字符串。与整数提升关系不大,因为每个字节都可以无损转换为长整型,但不是每个长整型都可以无损转换为字节。然而,即使是对象类型 Byte 和 Long 也不是相互继承的。
你只是没有收到警告,因为
A byte is a sequence of 8 bits, which makes 2^8 cases = 256. Half of them represent negative numbers, which is -128 to -1. Then there is the 0, and about the half, 1 to 127 represent the positive numbers.
130 as Int looks like 128 + 2 which is:
However, the Byte has just 8 digits, and the assignment takes just the bits as they are, but just the last ones:
The first bit indicates, it is a negative number. Now how much do you need to add to get to zero? Let's do it stepwise:
Lets do bigger steps. Just add 1s where we have zeros, but first we go back to x:
Now there is the turning point: we add another 1, and get zero (plus overflow)
If (x+y) + 1 = 0, x+y = -1. A minus 1 is, interestingly, not just the same as 1 (0000:0001) with a 'negative-flag' set ('1000:0001'), but looks completely different. However, the first position always tells you the sign: 1 always indicates negative.
But what did we add before?
It doesn't have a 1 at the first position, so it is a positive value. We know how to deconstruct that?
And now it's clear: x+125 = -1 => x = -126
You may imagine the values, organized in a circle, with the 0 at the top (high noon) and positive values arranged like on a clock from 0 to 5 (but to 127), and the turning point at the bottom (127 + 1 => -128 [sic!].) Now you can go on clockwise, adding 1 leads to -127, -126, -125, ... -3, -2, -1 (at 11 o'clock) and finally 0 at the top again.
For bigger numbers (small, int, long) take bigger clocks, with the zero always on top, the maximum and minimum always on bottom. But even a byte is much too big, to make a picture, so I made one of a nibble, a half-byte:
You can easily fill the holes in the picture, it's trivial!
Btw.: the whole thing isn't called casting. Casting is only used between Objects. If you have something, which is in real a subtype:
this is just an assignment, since a String is (always) an Object. No casting involved.
This is a casting. To the more specific type. Not every object is a String. There is a small relationship to integer promotion, since every byte can be lossless transformed to long, but not every long to byte. However, even Byte and Long, the Object-types, aren't inherited from each other.
You just don't get a warning, for
字节在 Java 中是有符号的 - 因此值的范围是 -128 到 127(包括 -128 和 127)。
当简单地截断为 8 位时,130 作为长整型的位模式就是 -126 作为字节的位模式。
另一个例子:
Bytes are signed in Java - so the range of values is -128 to 127 inclusive.
The bit pattern for 130 as a long, when simply truncated to 8 bits, is the bit pattern for -126 as a byte.
As another example:
你的意思是
byte b = (byte)l
?Java 的类型是有符号的,因此字节允许-128 到+127 之间的数字。
You mean
byte b = (byte)l
?Java's types are signed, so bytes allow numbers between -128 and +127.
供初学者理解:
1 字节 = 8 位
范围(源自 2 的补码编号系统)= [-2^(n-1) 到 2^(n-1)-1],其中 n 为编号。位
因此范围是 -128 到 127
每当值增加超过可能的最高 +ve 值时,流量就会转到可能的最低 -ve 值。
因此,在值达到 127 后,流程继续从 -128 到 -127 到 -126
覆盖总空间 130,因此 o/p 为 -126
For beginners to understand:
1 byte = 8 bits
Range (derived from 2's complement no. system) = [-2^(n-1) to 2^(n-1)-1], where n is no. of bits
So range is -128 to 127
Whenever value is incremented more than highest possible +ve value, the flow goes to the lowest possible -ve value.
So after value reaches 127 , flow continues from -128 to -127 to -126
to cover a total space of 130 and the thus o/p is -126
无符号字节保存的值是 0 到 255,因此数字 130 可以容纳在其中,但是当它是有符号字节时,它最多只能达到 127,因此 130 将无法正确存储。
由于数字以补码格式存储,因此如果数字大于 127,则可以减去 256。130 大于 127,因此 130 - 256 = -126。
另请记住,前 8 位之后的任何其他位都将丢失。如果你有一个long valLong = 257,即9位,那么当转换为字节时,它会丢失一些信息并变成值1(257作为long是00000000_00000000_00000000_00000000_00000000_00000000_00000001_00000001,但一个字节可以只保留最后一部分:00000001。
An unsigned byte holds values 0 to 255, so the number 130 can fit inside it, but when it's a signed byte, it can only go up to 127, so 130 isn't going to store properly.
Since the number is stored in Twos-complement format, you can subract 256 if the number is larger than 127. 130 is larger than 127 so 130 - 256 = -126.
Also keep in mind that any other bits past the first 8 will be lost. If you have a long valLong = 257, that is 9 bits, so when converting to a byte it loses some information and becomes a value of 1 (257 as a long is 00000000_00000000_00000000_00000000_00000000_00000000_00000001_00000001 but a byte can only hold the last part: 00000001.