1

I have a sequence of byte arrays of size 8 (64 bits). I want to put the corresponding natural number into a variable (of primitive type), for this reason i thought to use a double variable because the other primitive types do not support a so high range (from 0 to 2^64).

I show an example:

I have the following array of bytes

000000 0000000 0000000 0000000 0000000 0000000 0000000 0000010

the corresponding natural number is 2.

I have tried this:

double bigNaturalNum = new BigInteger(byteArray).doubleValue();

There problem is that the doubleValue() method returns the double value that the byte represents, thus the resulting value can be a decimal value.

asked Dec 7, 2019 at 15:32
5
  • 1
    Perhaps you want long (64-bit integer), rather than double (double-precision floating-point)? Commented Dec 7, 2019 at 15:40
  • 1
    Well, you gotta accept the fact that 2^64 can't be represented accurately by any of the primitives. That's what BigInteger is for. Do you want an long for when the number is within the range of long, and a double for when it exceeds the range? Commented Dec 7, 2019 at 15:47
  • 1
    Further to the other comments, long is 64-bit signed. There are no suitable unsigned primitives in Java (only the weird 16-bit char). So you need an object, or treat long as if it were unsigned knowing that it is signed. Commented Dec 7, 2019 at 15:51
  • If I store the number in a double variable and the value is natural, why I can not represent a 64 bit value? Commented Dec 7, 2019 at 15:59
  • If I store the number in a double variable and the value is natural, why I can not represent a 64 bit value?. Because you can't fit 64 bits into a 53-bit field. See double format. Commented Dec 7, 2019 at 16:48

2 Answers 2

1

This depends if the most significant bit in the byte[] is used as the sign bit e.g. two's complement. If so you can map byte[] to long with ByteBuffer:

byte[] arr = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
long l = ByteBuffer.wrap(arr).getLong();
System.out.println(l); // -1
System.out.println(Long.toHexString(l)); // ffffffffffffffff

If not you should use BigInteger with signum=1 to indicate that you don't use sign bit:

byte[] arr = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
BigInteger i = new BigInteger(1, arr);
System.out.println(i); // 18446744073709551615
answered Dec 7, 2019 at 15:45
Sign up to request clarification or add additional context in comments.

Comments

0

I have finally solved storing the value in a long variable. Java has not an unsigned long primitive type but from JavaSE 8, you can treat the long variable as an unsigned long variable through the methods of class Long Long - JavaSE 8.

You can use for instance the method public static int compareUnsigned(long x, long y) which

Compares two long values numerically treating the values as unsigned.

In this way you can store 2^64 natural nubers in a long variable.

answered Dec 7, 2019 at 20:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.