Java Float Array Create toFloatRawBits(int i)

Here you can find the source of toFloatRawBits(int i)

Description

Converts the raw bits of a 16-bit half float to the raw bits of a 32-bit float.

License

Open Source License

Parameter

Parameter Description
y the raw bits of a 16-bit half float

Return

the raw bits of a 32-bit float

Declaration

public static int toFloatRawBits(int i) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w ww .  ja va 2s  .  c o m
     * Delta between bias of 32-bit float and 16-bit half float. (127 - 15)
     * 
     * */
    public static int BIAS_DELTA = 127 - 15;

    /**
     * Converts the raw bits of a 16-bit half float to the raw bits 
     * of a 32-bit float.
     * 
     * @param y the raw bits of a 16-bit half float
     * @return the raw bits of a 32-bit float
     * 
     * */
    public static int toFloatRawBits(int i) {
        int s = (i >> 15) & 0x1;
        int e = (i >> 10) & 0x1f;
        int m = i & 0x3ff;

        if (e == 0) {
            if (m == 0) {
                // plus or minus zero
                return s << 31;
            } else {
                // denormalized number -- renormalize it
                while ((m & 0x400) == 0) {
                    m <<= 1;
                    e -= 1;
                }
                e += 1; // restore e offset with 1
                m &= ~0x400; // get rid of the implicit leading 1
            }
        } else if (e == 31) {
            if (m == 0) {
                // positive or negative infinity
                return (s << 31) | 0x7f800000;
            } else {
                // NaN -- preserve sign and significand bits
                return (s << 31) | 0x7f800000 | (m << 13);
            }
        }

        // normalized number
        e = e + BIAS_DELTA;
        m = m << 13;

        // Assemble s, e and m.
        return (s << 31) | (e << 23) | m;
    }
}

Related

  1. toFloatArray(String text, String delim)
  2. toFloatArray(String[] anArray)
  3. toFloatArrays(double[][] arrays)
  4. toFloatByObject(Object obj)
  5. toFloatMatrix(int[][] m)
  6. toFloats(byte[] bytes)
  7. toFloats(byte[] value, int offset, int num)
  8. toFloats(double[] d)
  9. toFloats(final double[] array)