Convert float to bit array in Java
Description
The following code shows how to convert float to bit array.
Example
//from w w w . j av a2 s . c o m
public class Main {
public static String toBitString(final float f) {
final char[] bit = new char[32];
final int ff = Float.floatToIntBits(f);
int mask = 1;
for(int i = 0; i < 32; i++) {
final int bitval = ff & mask;
if(bitval == 0) {
bit[31 - i] = '0';
} else {
bit[31 - i] = '1';
}
mask <<= 1;
}
return String.valueOf(bit);
}
public static void main(String[] argv){
System.out.println(toBitString(123.123F));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »