1


With reference to (Double to byte[]) It contains both the question and the solution I require.

private byte[] convert(Double d) {
    byte[] output = new byte[8];
    Long lng = Double.doubleToLongBits(d);
    for (int i = 0; i < 8; i++) {
        output[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
    }
    return output;
}

But I don't know how/why it works, I understand the loop is assigning each of the 8 bytes that make up a Double to the array. but I don't understand how the byte is being constructed. Could someone please explain to me the following expression?

 ((lng >> ((7 - i) * 8)) & 0xff);

and why is Double.doubleToLongBits(d); getting involved?

1
  • Are you asking what the operators do? Commented Nov 24, 2013 at 15:55

3 Answers 3

2

Let's break down this expression:

((lng >> ((7 - i) * 8)) & 0xff);

step by step. ((7 - i) * 8) should be obvious. >> is the binary right shift operator. It rotates its left operand by right operand number of bits filling the MSB from the left. So, lng is rotated by (7-i) number of bytes on each iteration.

& is the bitwise AND operator which means that if you have a 1 in the same bit position for both the operands, you get a 1, otherwise 0. So, basically ANDing with 0xff basically gives us the last byte of the rotated lng.

To put it all together, you get each byte of lng starting from the most significant byte in each iteration.

To answer the second part of your question, try rotating the bits of the double value directly (You'll get a nasty error). In Java, double and long use the same number of bytes(8) and so if you want to carry out bitwise operations, you have to convert double values to long first.

Sign up to request clarification or add additional context in comments.

3 Comments

No, >> fills with the value of the MSB. Use >>> if you want 0's.
*8 because 8bits in Byte. 7-i because the Byte position we want to focus on. >> Rotates the long around by ((7-1)*8) number of bit positions. & 0xff because 255 is max value of a Byte and the "overlap" between the last byte is what I want. Correct?
All correct except for this: & 0xff because it is all ones. 255 is 0x7f - first bit 0 and rest 1.
0

The idea is to shift an integral value to the right (this is the right shift operator >>) and then take the last 8 bits with (& 0xff).

For example, if you want the 3rd byte from the right of a long n

(n >> (2*8)) & 0xff

The doubleToLongBits is needed because you can't shift the bits of a floating point value, only of integral values. Hence, this method just gives you the bit pattern of a double as long value.

Comments

0

Double#doubleToLongBits creates a 64-bit representation of the value in IEEE 754 format.

The loop takes all 64-bits, starting with the most significant bit, and stores them in an 8-byte array.

lng = 0x123456789abcdef0

(byte)((lng >> ((7 - 0) * 8)) & 0xff) == (byte) 0x12
(byte)((lng >> ((7 - 1) * 8)) & 0xff) == (byte) 0x34
(byte)((lng >> ((7 - 2) * 8)) & 0xff) == (byte) 0x56
(byte)((lng >> ((7 - 3) * 8)) & 0xff) == (byte) 0x78
(byte)((lng >> ((7 - 4) * 8)) & 0xff) == (byte) 0x9a
(byte)((lng >> ((7 - 5) * 8)) & 0xff) == (byte) 0xbc
(byte)((lng >> ((7 - 6) * 8)) & 0xff) == (byte) 0xde
(byte)((lng >> ((7 - 7) * 8)) & 0xff) == (byte) 0xf0

Comments

Your Answer

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.