如果我在Java中将十进制127转换为十六进制,则为7f。
我可以使用以下方法获得相同的输出:
(1 << 7) - 1 = 127
0x7F = 127
我正在尝试转换十进制值36028797018963967
。
以十六进制表示:
0x7fffffffffffff
我该如何用位移来表示?例如,输出应为(填写?
):
(1 << ?) - 1 = 36028797018963967
是否有任何Java代码以这种格式获取输出?
答案 0 :(得分:2)
您可以使用long
或BigInteger
。
public void test() {
// Shifting to get the number.
long l = (1L << 63) - 1L;
System.out.println("" + l + " -> " + Long.toHexString(l));
BigInteger bi = BigInteger.ONE.shiftLeft(63).subtract(BigInteger.ONE);
System.out.println("" + bi + " -> " + bi.toString(16));
bi = BigInteger.ONE.shiftLeft(55).subtract(BigInteger.ONE);
System.out.println("" + bi + " -> " + bi.toString(16));
// Finding the amount to shift - it's easiest using BigInteger
System.out.println(bi.bitLength());
}
打印
9223372036854775807 -> 7fffffffffffffff
9223372036854775807 -> 7fffffffffffffff
36028797018963967 -> 7fffffffffffff
55
答案 1 :(得分:2)
System.out.println((1L << 55) - 1); //36028797018963967