如何将float的位重新解释为int

ivo*_*der 10 c++ java casting

以下C++代码的Java等价物是什么?

 float f=12.5f;
 int& i = reinterpret_cast<int&>(f);
Run Code Online (Sandbox Code Playgroud)

mis*_*tor 26

float f = 12.5f;
int i = Float.floatToIntBits(f);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,C++版本使用int引用.因此修改float将导致int更改(以某种未定义的方式).Java版本不提供该功能.每次更改为'f'后,都必须调用floatToIntBits(). (12认同)
  • @Montecristo:Java中的(int)f类似于C++中的static_cast <int>(f). (3认同)