假设我在java中有一个 float 。如何以二进制形式打印它? java库中有没有简单的方法可以使用?我说的是 IEEE 754 浮点。或者我必须从头开始编写它?
我也想打印出来。
最佳答案
Integer.toBinaryString(Float.floatToIntBits(FloatNumber));
示例:
class FloatToBinary {
public static void main( String ... args ) {
int BitRep = Float.floatToIntBits(12345.12346f); //Bit Representation of the Float
System.out.println(BitRep); //1178657918
String BinString = Integer.toBinaryString(BitRep); //Binary representation in String
System.out.println(BinString); //1000110010000001110010001111110
}
}
编辑
看起来相同的问题已经得到回答: how to convert a string of binary values to a float and vice-versa
关于java - 有没有办法以二进制形式显示 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252025/