Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

128 bit representation and primitives in java

I need to represent 128 bit int key in java, like

0x9c1f03a0d9cf510f2765bd0f226ff5dc

I know how represent 128 bit variable in theory.. cut into 2 64 bit int or four 32 bit 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..

I am totally ignorant with bit manipulation and transformation, some explanations would be very useful

like image 400
Master AI Avatar asked Mar 26 '26 16:03

Master AI


1 Answers

Fantastic news! Java provides an arbitrary precision integral type. The BigInteger(String, int) constructor can be used to take your hex and make a 128-bit value. Further BigInteger is Comparable. 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
like image 135
Elliott Frisch Avatar answered Mar 29 '26 04:03

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!