Java十六进制计算
Java hex calculation
我有 long
值 bits
声明如下:
long bits = len*8L;
(304)
System.out.println(bits);
这输出为 304
如果我像这样使用长名称位,我将分别得到 0 和 0。
System.out.println(bits>>(4*8));
System.out.println(0xFF&(bits>>(4*8)));
如果我使用实际数字,像这样,我分别得到 304 和 48
System.out.println(304>>(4*8));
System.out.println(0xFF&(304>>(4*8)));
我正在尝试将此 Java 转换为 JavaScript,但 JavaScript 在所有情况下都为我提供 304 和 48。我需要它来匹配 Java 并给出 0 和 0.
的值
编辑
跟进,为了清楚起见,我需要 Java 相当于等于 0 的脚本,模仿 Java 目前的做法(上面两个等于 0 的例子不会我们正在开发的内容发生了变化)。
所以console.log(0xFF&(bits>>(4*8)))应该等于0,目前等于48
JLS, Section 15.19 涵盖了 Java 中的移位运算符。
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 value 0x1f
(0b11111
). The shift distance actually used is therefore always in the range 0
to 31
, inclusive.
对于 int
值,例如 304
,4*8
或 32 的移位值实际上是 0
,因此不会发生移位。然后有点 - 并且 0xFF
产生 48
.
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 value 0x3f
(0b111111
). The shift distance actually used is therefore always in the range 0
to 63
, inclusive.
对于 long
值,4*8
的移位值确实向右移动了 32 位,从而产生 0
.
This page 涵盖 Java 脚本位移运算符。
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers.
似乎 Java脚本将数字转换为 32 位数字,如 Java int
。同样的 "only the least 5 bits" 规则似乎也适用于 JavaScript 中的移位操作数。
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 中的常量获得与变量相同的结果,你需要将 304 作为长常量传递给 304L
,如下所示:
System.out.println(304L>>(4*8));
System.out.println(0xFF&(304L>>(4*8)));
原因是你不能用 4*8=32 位移动一个 int
; Java 将移动 32 模 32 = 零但是,因为 int
只有 32 位长。
相反,Java脚本不支持使用 >>
运算符移动 64 位整数;它将您传递给 >>
的每个数字都视为 32 位整数。
您可以编写自己的函数来执行类似的操作:
function rshift(num, bits) {
return Math.round(num / Math.pow(2,bits));
}
console.log(rshift(304, 4*8))
我有 long
值 bits
声明如下:
long bits = len*8L;
(304)
System.out.println(bits);
这输出为 304
如果我像这样使用长名称位,我将分别得到 0 和 0。
System.out.println(bits>>(4*8));
System.out.println(0xFF&(bits>>(4*8)));
如果我使用实际数字,像这样,我分别得到 304 和 48
System.out.println(304>>(4*8));
System.out.println(0xFF&(304>>(4*8)));
我正在尝试将此 Java 转换为 JavaScript,但 JavaScript 在所有情况下都为我提供 304 和 48。我需要它来匹配 Java 并给出 0 和 0.
的值编辑
跟进,为了清楚起见,我需要 Java 相当于等于 0 的脚本,模仿 Java 目前的做法(上面两个等于 0 的例子不会我们正在开发的内容发生了变化)。
所以console.log(0xFF&(bits>>(4*8)))应该等于0,目前等于48
JLS, Section 15.19 涵盖了 Java 中的移位运算符。
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
.
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
.
This page 涵盖 Java 脚本位移运算符。
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers.
似乎 Java脚本将数字转换为 32 位数字,如 Java int
。同样的 "only the least 5 bits" 规则似乎也适用于 JavaScript 中的移位操作数。
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 中的常量获得与变量相同的结果,你需要将 304 作为长常量传递给 304L
,如下所示:
System.out.println(304L>>(4*8));
System.out.println(0xFF&(304L>>(4*8)));
原因是你不能用 4*8=32 位移动一个 int
; Java 将移动 32 模 32 = 零但是,因为 int
只有 32 位长。
Java脚本不支持使用 >>
运算符移动 64 位整数;它将您传递给 >>
的每个数字都视为 32 位整数。
您可以编写自己的函数来执行类似的操作:
function rshift(num, bits) {
return Math.round(num / Math.pow(2,bits));
}
console.log(rshift(304, 4*8))