c++ - Float bitwise ops -


I want to convert a floating point number store into an integer ('number' value is not required to be equal) .

If a floating point number (like 10) is represented in binary (at least on my implementation):

  010000010010000000000000000000000000   

Then it should represent the integer value 1092616192 (on my execution).

I am currently doing this through:

  union UNFI {float a; Int b; }; UNFI N; NA A = 10; Int bits = n.b; // Foo! For (int i = 31; i> = 0; -i) {bull bit = ((bits> gt; & gt; & amp; 1); Std :: cout & lt; & Lt; Bit; }   

That which I like, is receiving, but it is in its nature, undefined behavior. So I would like to know what is the 'right' way of achieving this result.

With the exception of CMT:

  With the exception of a member, the value of the member is used when a union object is used when the most recent store for the object is a different The member was, the behavior is implementation-defined.   

Is not this undefined behavior?

The correct way to call > memcpy . Most compilers will optimize this efficient single-word memory access as a union, but with additional alignment requirements on the platform, they will do the right thing and it will never trigger the signal nan.

  float A = 10; Four bits [size aof]; Mempie (bits, and a, saifose a);   

or

  int b; Static_assert (sizeof b == size aof); Mempi (and B, A, Size A);    

Comments