How to assign float variable to unsigned int variable, bitmap, and not distinguish

I know this is a strange thing and it is not portable. But I have a dedicated array of unsigned ints, and sometimes I want to "store" the float in it. I do not want to use a float or convert it to the nearest equivalent int; I want to save the exact bitmap of the float in the allocated unsigned int space, so that later I can get it as a float and keep its original float value.

+4
source share
3 answers

This can be achieved with a simple copy:

uint32_t dst; float src = get_float(); char * const p = reinterpret_cast<char*>(&dst); std::copy(p, p + sizeof(float), reinterpret_cast<char *>(&src)); // now read dst 

Copying back works the same way.

+6
source

Just re-interpret from the appropriate memory location:

 float f = 0.5f; unsigned int i = *reinterpret_cast<unsigned int*>(&f); 

or more C-shaped version:

 unsigned int i = *(unsigned int*)&f; 

From your question text, I assume that you know that this is interrupted if the float and unsigned int not the same size, but on most common platforms both should be 32-bit.

EDIT: As Kerrek pointed out, this looks like undefined. But I still adhere to my answer, because it is short and accurate and should really work on any practical compiler (convince me otherwise). But look at Kerrek's answer if you want a UB-free answer.

+2
source

You can use reinterpret_cast if you really need to. You don’t even have to play with pointers / addresses, as other answers mention. for instance

 int i; reinterpret_cast<float&>(i) = 10; std::cout << std::endl << i << " " << reinterpret_cast<float&>(i) << std::endl; 

also works (and prints 1092616192 10 if you are qurious;).

EDIT:

From the C ++ standard (about reinterpret_cast):

5.2.10.7 A pointer to an object can be explicitly converted to a pointer to an object of another type. Except that the conversion of the rvalue type from "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment requirements of T2 are no more stringent than that of T1) and back to its original type is original the value of the pointer, the result of such a pointer conversion is not defined.

5.2.10.10 10 The lvalue expression of type T1 can be passed to the type "reference to T2" if the expression of the type "pointer to T1" can be explicitly converted to the type "pointer to T2" using reinterpret_cast. That is, the reinterpret_cast<T&>(x) link has the same effect as the *reinterpret_cast<T*>(&x) conversion with built-in and * operators. The result is an lvalue that references the same object as the lvalue source, but with a different type. Temporary creation is not created, the copy is missing (12.1) or the conversion functions (12.3) are not called.67)

Thus, it seems that consecutive reinterpretation pointers are not undefined behavior, and using links has the same result as the resulting address, redial, and deferral pointer. I still claim that this is not undefined behavior.

0
source

Source: https://habr.com/ru/post/1238378/


All Articles