views:

1174

answers:

3

** first off I should ask: **
Does anyone knows of a current implementation 126b 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 think 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 ?

+2  A: 

Why not use BigInteger?

Visage
BigInteger is _slow_ when all you need is just a little over 64 bits.. Ran into this problem a year ago and turned out to be 25 times slower than the original long. See this anwer for details: http://stackoverflow.com/questions/962747/most-shameful-awesome-language-hack/1084538#1084538
Tim
Odd that this is the accepted answer, considering the OP said he didn't want BigInteger.
Ira Baxter
+2  A: 

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.

starblue
You can use 64 bit longs just fine ==> twice as fast. A carry out can be determined by changes to the sign bit.
Ira Baxter
@Ira Baxter I doubt it would be faster. It would be possible but more complicated for addition, but not for multiplication. Java BigInteger uses int[], and I suppose they know what they are doing.
starblue
If you want a very high performance BigInt package, you use the largest wordsize available to your machine for which there are native machine instruction support. Its hard to find a PC these days that isn't 64 bits. I stand by my position: use a long. The algorithms in the BigInt package are probably typical of most multiprecision packages; long should drop relatively easily into place. Additions of large bigints now simply take half as many cycles. Multiplications should be 4x as fast because you only need 1 product instead of 4 half-wide cross products.
Ira Baxter
I might soften this a little: you want the largest set of bits for which you can get a double-precision product from two single-precision multiplies. Java ints cast as longs satisfy this in a Java-independent way, which sort of justifies your answer. If you need speed, you'll drop into native machine code. On the x86, the FP unit offers 80 bit precision minimum which implies you might want to choose 40-bit "digits".
Ira Baxter
+1  A: 

Don't tell me that you plan to have 128 static setters and getters, one for each bit??? I'd definitively go for setBit(int index, boolean value) and getBit(int index) as instance methods.

More things you need: a toString() method so you can get a human readable representation (at some point you will want to print the numbers, I think).

Remember that all the ordinal types in java are signed (with the exception of char), so if you plan to use two longs, keep always in mind that the lower part could be problematic for detecting overflows and such... anyway, you will have a 127 bit number unless because the lower part would be treated as a 63 bit unsigned.

fortran
Where did the OP even hint at setters for each bit?
Ira Baxter
http://stackoverflow.com/revisions/1096964/list you should take a look before criticizing.
fortran
OK, now I see it. I didn't expect I'd have to read the revisions of a question to understand it; that seems a bit over the top. I've undinged your response.
Ira Baxter
not to understand it, but don't expect people coming back to change their answers everytime the question is updated (among other things because it's not notified)...
fortran