简体   繁体   English

Java中的128位表示形式和原语

[英]128 bit representation and primitives in java

I need to represent 128 bit int key in java, like 我需要在Java中表示128位int密钥,例如

0x9c1f03a0d9cf510f2765bd0f226ff5dc

I know how represent 128 bit variable in theory.. cut into 2 64 bit int or four 32 bit int. 我知道理论上如何表示128位变量。切成2个64位int或四个32位int。

But i need this representation for compare keys (k1 < k2 and k1 == k2) and i dont know how doing that with a key splitted into severals int, and i dont know how split my hexa key into 2 or 4 int either.. 但是我需要比较键(k1 <k2和k1 == k2)的这种表示形式,而且我不知道如何将键拆分成若干个int来做到这一点,而且我也不知道如何将我的六角键拆分成2或4个int。

I am totally ignorant with bit manipulation and transformation, some explanations would be very useful 我对位操作和转换一无所知,有些解释将非常有用

Fantastic news! 好消息! Java provides an arbitrary precision integral type. Java提供了一个任意精度的整数类型。 The BigInteger(String, int) constructor can be used to take your hex and make a 128-bit value. BigInteger(String, int)构造函数可用于获取十六进制并生成128位值。 Further BigInteger is Comparable . BigIntegerComparable You could use it like, 您可以像这样使用它

BigInteger bi = new BigInteger("9c1f03a0d9cf510f2765bd0f226ff5dc", 16);
BigInteger bi2 = bi.add(BigInteger.ONE);
if (bi2.compareTo(bi) > 0) {
    System.out.println("Like this");
}

Outputs 产出

Like this

With Long.compareUnsigned (and other methods that treat long s as unsigned), bit tricks aren't essential anymore. 使用Long.compareUnsigned (以及将long s视为无符号的其他方法),不再需要位技巧。 You can just implement a standard multi-element comparison, where the more-significant values are handled first. 您可以只实施标准的多元素比较,首先比较重要的值。

You should use long s in preference to int s, though, since that will significantly reduce work done by 64-bit CPUs while not having much difference for 32-bit CPUs. 不过,您应该优先使用long s而不是int ,因为这将大大减少64位CPU的工作量,而对于32位CPU则没有太大的区别。


For compareTo with long[] s in little-endian: 对于little-endian中带有long[]compareTo

public static int keyCompareTo(final long[] a, final long[] b) {
    final int highComp = Long.compareUnsigned(a[1], b[1]);
    if (highComp != 0) return highComp;
    else return Long.compareUnsigned(a[0], b[0]);
}

Or, with an object: 或者,与一个对象:

public class Key implements Comparable<Key> {
    final protected long high;
    final protected long low;

    public int compareTo(final Key other) {
        if (other == null) throw new NullPointerException();
        final int highComp = Long.compareUnsigned(a.high, b.high);
        if (highComp != 0) return highComp;
        else return Long.compareUnsigned(a.low, b.low);
    }
}

For equality: 为了平等:

a[0] == b[0] && a[1] == b[1]
a.high == b.high && a.low == b.low

For less-than: 少于:

final int highComp = Long.compareUnsigned(a[1], b[1]);
final boolean lessThan = highComp < 0 || (highComp == 0 && Long.compareUnsigned(a[0], b[0]) < 0);
final int highComp = Long.compareUnsigned(a.high, b.high);
final boolean lessThan = highComp < 0 || (highComp == 0 && Long.compareUnsigned(a.low, b.low) < 0);

You could use BigInteger . 您可以使用BigInteger

String hexString = "9c1f03a0d9cf510f2765bd0f226ff5dc";
BigInteger bigInt = new BigInteger(hexString, 16);
System.out.println(bigInt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM