tags:

views:

1469

answers:

6

I am looking for one datatype with exact capacity of 0 to 2^64 - 1. We know that Java as it is does not support 'unsigned' barring char datatype.

There is BigInteger class that allows creation of larger numbers that long datatype cannot support. But I am not sure how BigInteger class would serve my purpose. BigInteger class allows assignment through constructors only. I see the following possibility but it generates a random number.

BigInteger(int numBits, Random rnd) 
Constructs a randomly generated BigInteger, uniformly distributed over the range 0 to (2^numBits - 1), inclusive.

I can't see any setValue(x) kind of API that'd let me choose my own value to this BigInteger. How to implement this using BigInteger class or is there any other way of doing it? Please post code sample.

PS: The question posted by someone here does not have implementation details.

A: 

BigIntegers are immutable, just as you've found. You might want to look into subclassing BigInteger, and writing your own constructor that validates input, and emits a positive BigInteger in the relevant range.

To maintain the requirement that numbers are only using 64 bits, you might need to overload the various operations too, so they limit the result and return an instance of your new class rather than a new BigInteger.

This is probably quite a bit of work, but should still be far better than doing it all from scratch.

unwind
A: 

You can create a BigInteger from a long with BigInteger.valueOf(l), where l is a long.

But if you want to work with exact 64-bits, I'd use just long.

Ingo
+2  A: 

Why not write your own wrapper and underneath use a signed long. If the user wishes to fetch the unsigned value as a BigInteger - test the sign and add 2^64 to the BigInteger.

mP
A: 

you may be want to create a UInt64 class, that encapsulate a BigInteger; you can also check that every operation (add, mul, etc) returns an unsigned 64 bit BigInteger; simulating the overflow maybe tricky

class UInt64 {

    private final BigInteger value;

    private UInt64(BigInteger aValue) {
         // method to enforce your class invariant: 0...2**64-1
         checkInvariantOf(aValue);
         value = aValue; 
    }

    public static UInt64 of(String value) {
         return new UInt64(new BigInteger(value));
    }

    public UInt64 add(UInt64 v) {
         return new UInt64(value.add(v.value));
    }

    ....
}
dfa
+1  A: 

You can often use Java's signed numeric data types as if they were unsigned.

See this old answer about signed vs. unsigned in Java.

starblue
A: 

You can store values 0 to 2^64-1 in a long value.

Many operations work as expected however most of the APIs and some of the operations only work as they assume signed operations however there are workarounds.

Using BigInteger may be simpler to get your head around however. ;)

Peter Lawrey
long supports -2^63 to (2^63)-1 as first bit is the sign bit. I want unsigned long which would then be ranging from 0 to (2^64)-1. It's not 2^(64-1) that I want. It is (2^64)-1.
Chandan .
Peter Lawrey
Also == and != ;)
Peter Lawrey