What does (int &) conversion mean in C ++

float b = 1.0f; int i = (int)b; int& j = (int&)b; cout << i << endl; cout << j << end; 

Then pin i was 1 , and pin 1065353216 ! This is a big surprise for me! So what is the true meaning of the conversion (int&) ?

+6
c ++
source share
6 answers

This is a C style cast problem. You should carefully look at what you get. In your case, "(int)" was a regular static throw. The value is converted to int by truncation. In your case, "(int &)" was reinterpreted. The result is an lvalue that refers to memory cell b, but is treated as an int. This is actually a violation of strict anti-aliasing rules. Therefore, do not be surprised if your code will no longer work after turning on all optimizations.

Equivalent code using C ++ style styles:

 float b = 1.0f; int i = static_cast<int>(b); int& j = reinterpret_cast<int&>(b); cout<<i<<endl; cout<<j<<end; 

Check out your favorite C ++ book for these kinds of casts.

+18
source share

In hexadecimal, 1065353216 is 0x3F800000. If you interpret this as a 32 bit floating point number, you will get 1.0. If you write it in binary format, you will get the following:

  3 F 8 0 0 0 0 0
 0011 1111 1000 0000 0000 0000 0000 0000

Or grouped in different ways:

  0 01111111 00000000000000000000000
 s eeeeeeee vvvvvvvvvvvvvvvvvvvvvvvvv

The first bit ( s ) is the sign bit, the next 8 bits ( e ) are exponential, and the last 23 bits ( v ) are significant. "A single precision binary floating-point exponent is encoded using the binary representation of the offset at zero offset 127, also known as the exponent offset in IEEE 754. " Interpreting this, you see that the sign is 0 (positive), the exponent is 0 (01111111 b = 127, "zero offset"), and the value is 0. This gives you +0 0 which is 1.0.

In any case, what happens is that you take a reference to float ( b ) and re-interpret it as a reference to int (int&) . Therefore, when you read the value of j , you get a bit from b . Interpreted as float, these bits mean 1.0, but are interpreted as int, these bits mean 1065353216.

For what it's worth, I never used cast, using & , like (int&) . I would not expect to see this or use it in any normal C ++ code.

+11
source share
 float b = 1.0f; ... int& j = (int&)b; 

In the second conversion, you look at the memory space that contains b, as if it were a memory space containing int. Floating point values ​​are stored as completely different integers, so the results are really different ...

+2
source share

In this particular case, the transformation in question does not matter. This is an attempt to rethink the memory occupied by the float and int Lvalue. This is explicitly forbidden in C / C ++, which means that it creates undefined behavior. undefined behavior is the only meaning it has in this case.

+2
source share

It looks like you are trying to create an int reference to a float using (int &) cast. This will not work, since floats are presented differently than int. This will not work.

If the views of float and int are the same, this might work.

+1
source share

What are you going to do? Same:

 float b = 1.0f; int i = (int) b; int* j = (int*)b;//here we treat b as a pointer to an integer cout<<i<<endl; cout<<(*j)<<endl; 

How to fix:

 float b = 1.0f; int i = (int) b; int castedB = (int)b;//static_cast<int>(b); int& j = castedB; cout<<i<<endl; cout<<j<<endl; 
+1
source share

All Articles