java 如何在浮点数中获取/设置单个位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1310491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:04:24  来源:igfitidea点击:

How can I get/set individual bits in a float?

javafloating-point

提问by paxdiablo

I have to admit in all my work with Java, I've never come across the need for a Java union (like the C union, not the SQL one) and I cannot find an answer here on SO. Admittedly, most of my work in Java has been at higher abstractions than bit-fiddling.

我不得不承认,在我所有的 Java 工作中,我从未遇到过需要 Java 联合(如 C 联合,而不是 SQL 联合),而且我无法在 SO 上找到答案。诚然,我在 Java 方面的大部分工作都处于更高的抽象阶段,而不是位面摆弄。

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

我有一个整数,我正在为其设置单个位,我想打印出等效的 IEEE754 单精度浮点数。

In C, I'd do something like:

在 C 中,我会做类似的事情:

union {
    int i;
    float f;
} x;
x.i = 0x27;
printf ("%f\n", x.f);

How do I do a similar thing in Java? Is it even possible to treat the same memory as two different data types in Java?

我如何在 Java 中做类似的事情?甚至可以将相同的内存视为 Java 中的两种不同数据类型吗?

I did search on both SO and elsewhere for "java union" but it swamped me with SQL stuff - I couldn't find a way to do this.

我确实在 SO 和其他地方搜索了“java union”,但它让我充满了 SQL 的东西——我找不到办法做到这一点。

回答by Michael Borgwardt

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

我有一个整数,我正在为其设置单个位,我想打印出等效的 IEEE754 单精度浮点数。

Use intBitsToFloat()for that.

为此使用intBitsToFloat()

回答by skaffman

Substitutes for Missing C Constructs

替换缺失的 C 结构

The designers of the Java programming language chose to omit the union construct because there is a much better mechanism for defining a single data type capable of representing objects of various types: subtyping. A discriminated union is really just a pallid imitation of a class hierarchy.

Java 编程语言的设计者选择省略联合构造,因为有一种更好的机制来定义能够表示各种类型对象的单一数据类型:子类型。有区别的联合实际上只是对类层次结构的苍白模仿。

Includes examples.

包括示例。

回答by Pavel Minaev

Java doesn't provide any way to treat a value as another value on raw bit level - no unions, nothing like reinterpret_cast. However, for your particular case of treating floatas intand vice versa, you can use java.lang.Float.floatToIntBitsand intBitsToFloatmethods.

Java 没有提供任何方法将一个值视为原始位级别的另一个值 - 没有联合,没有像reinterpret_cast. 但是,对于您对待floatasint和反之亦然的特殊情况,您可以使用java.lang.Float.floatToIntBitsintBitsToFloat方法。

回答by mfx

If you are interested in accessing the bit-level representation of a float, java.lang.Double contains method to do this (doubleToLongBits etc.)

如果您对访问浮点数的位级表示感兴趣,java.lang.Double 包含执行此操作的方法(doubleToLongBits 等)

Union'ing between pointer types (or between pointers and their numeric represetantion) would open holes in the type system, so it is strictly impossible.

指针类型之间(或指针和它们的数字表示之间)的联合会在类型系统中打开漏洞,所以这是绝对不可能的。

回答by adatapost

Some useful articles & faq to this subject are,

关于这个主题的一些有用的文章和常见问题是,

Union types in Java?

Java中的联合类型?

Substitutes for Missing C Constructs

替换缺失的 C 结构