Java十六进制计算 [英] Java hex calculation
问题描述
我这样声明了long
值bits
:
long bits = len*8L;
(304)
System.out.println(bits);
输出为304
如果我使用长名称位,则得到0&分别为0.
If I use the long name bits like so I get 0 & 0 respectively.
System.out.println(bits>>(4*8));
System.out.println(0xFF&(bits>>(4*8)));
如果我使用实际数字,则分别为304和48
If I use the actual number, like so, I get 304 and 48 respectively
System.out.println(304>>(4*8));
System.out.println(0xFF&(304>>(4*8)));
我正在尝试将此Java转换为JavaScript,但是JavaScript在所有情况下都为我提供了304和48.我需要它来匹配Java并赋予0和amp;值. 0.
I'm trying to convert this Java to JavaScript but JavaScript gives me 304 and 48 in all scenarios. I need it to match the Java and give values of 0 & 0.
有什么想法吗?
编辑
接下来,为了清楚起见,我需要JavaScript等于0,以模仿Java当前的方式(等于0的两个示例在我们正在开发的程序中不会更改).
Follow up, just to be clear, I need the JavaScript equivalent to equal 0, mimicking how the Java currently does it (the two examples above that equal 0 won't be changed in what we're developing).
因此console.log(0xFF&(bits >>(4 * 8)))应该等于0,当前等于48
So console.log(0xFF&(bits>>(4*8))) should equal 0, it currently equates to 48
推荐答案
The JLS, Section 15.19 covers shifting operators in Java.
如果左侧操作数的提升类型为
int
,则仅将右侧操作数的最低5位用作移位距离.就像右手操作数受到了按位逻辑AND运算符&的约束. (第15.22.1节)的掩码值0x1f
(0b11111
).因此,实际使用的移位距离始终在0
至31
的范围内.
If the promoted type of the left-hand operand is
int
, then only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value0x1f
(0b11111
). The shift distance actually used is therefore always in the range0
to31
, inclusive.
对于int
值(例如304
),4*8
或32的移位值实际上是0
,因此不会发生移位.然后用0xFF
进行位运算,得到48
.
For an int
value such as 304
, the bit shift value of 4*8
, or 32, is really 0
, so no shifting takes place. Then a bit-and with 0xFF
yields 48
.
如果左操作数的提升类型为
long
,则仅右操作数的六个最低阶位用作移位距离.就像右手操作数受到了按位逻辑AND运算符&的约束. (§15.22.1),其掩码值为0x3f
(0b111111
).因此,实际使用的移位距离始终在0
至63
的范围内(包括端点值).
If the promoted type of the left-hand operand is
long
, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value0x3f
(0b111111
). The shift distance actually used is therefore always in the range0
to63
, inclusive.
对于long
值,4*8
的移位值实际上确实会向右移32位,从而产生0
.
For a long
value, the bit shift value of 4*8
really does shift to the right 32 bits, which yields 0
.
此页面涵盖了JavaScript位移位运算符.
This page covers JavaScript bit-shift operators.
按位运算符将其操作数视为32位(零和一)的序列,而不是十进制,十六进制或八进制数字.
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers.
似乎JavaScript将数字转换为32位数字,例如Java int
.似乎同样的仅最低5位"规则也适用于JavaScript中的移位操作数.
It appears that JavaScript converts the number to a 32-bit number, like a Java int
. It also appears that the same "only the least 5 bits" rule applies to the shift operand in JavaScript also.
console.log(304>>32); // Don't shift!
console.log(0xFF&(304>>32)); // Don't shift!
console.log(304>>33); // Shift by 1, not 33
console.log(0xFF&(304>>33)); // Shift by 1, not 33
这篇关于Java十六进制计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!