Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reinterpret the bits of a float as an int

Tags:

java

c++

casting

What is the Java equivalent of following C++ code?

 float f=12.5f;
 int& i = reinterpret_cast<int&>(f);
like image 353
ivorykoder Avatar asked Jan 01 '10 16:01

ivorykoder


People also ask

Can you assign a float to an int?

A float value can be assigned to an integer variable but an implicit conversion occurs when compiler forces a float value to be assigned as an integer. The digits after the decimal notation in the float value get lost after assigning a float to an integer.

What is Reinterpret_cast in C?

reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different. It does not check if the pointer type and data pointed by the pointer is same or not.


1 Answers

float f = 12.5f;
int i = Float.floatToIntBits(f);
like image 193
missingfaktor Avatar answered Oct 01 '22 15:10

missingfaktor