double & bits

public class HDTest {

	public static void main(String[] args) {
		System.out.println(Arrays.toString(getBytes( 0.123456789 )));
		
		System.out.println(getBits( 0.123456789 ));
		System.out.println(getBits( 1.123456789 ));
		System.out.println(getBits( 0.2 ));
		System.out.println(getBits( 0.1 ));
	}

	private static byte[] getBytes(double value) {
		final long v = Double.doubleToLongBits(value);
		final byte[] vb = new byte[8];
		vb[7] = (byte) (v >>> 0);
		vb[6] = (byte) (v >>> 8);
		vb[5] = (byte) (v >>> 16);
		vb[4] = (byte) (v >>> 24);
		vb[3] = (byte) (v >>> 32);
		vb[2] = (byte) (v >>> 40);
		vb[1] = (byte) (v >>> 48);
		vb[0] = (byte) (v >>> 56);
		return vb;
	}
	
	private static String getBits(double value) {
		final long v = Double.doubleToLongBits(value);
		StringBuilder sb = new StringBuilder(64);
		for(int p=0; p<64; p++){
			sb.append((v >>> 63-p) & 1);
		}
		return sb.toString();
	}

	
	
}




你可能感兴趣的:(double & bits)