Java: Implementing a Unsigned 128bit Integer

Ande Turner picture Ande Turner · Jul 8, 2009 · Viewed 13.9k times · Source

first off I should ask:
Does anyone knows of a current implementation 128b UINT for Java?

I need something to hold natural cardinal values. ie: A huge counter.
I know of BigIntegers, which are slow and immutable. A 128b UINT makes sense ...

I was thinking about implementing an OWORD, using a pair of primitive longs.

Overflows would throw an Exception and not Wraparound.

What example sourcecode/blogs should I look to for implementing the workings of this class?

Answer

starblue picture starblue · Jul 8, 2009

I would use 32 bit integers as the representation, because you need a bigger type (long) to get the extra precision for the carry bit, overflow detection and multiplication. Think of a 32 bit integer as a digit and apply the algorithms from primary school.