L
L
LoonTiG2016-04-23 11:29:14
C++ / C#
LoonTiG, 2016-04-23 11:29:14

What does int i = *(int*)&x; mean?

float interesting_function(float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}

Can you help me figure out what the designation of variables in this format means?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Stolyarov, 2016-04-23
@Ni55aN

&x- the address (pointer) is taken, at which the value of the variable x is located
(int*)- the designation that with the value of the address that was received earlier, you need to work as a inttype (although it was earlier float)
* - we get the value at this address, but it will no longer be float, a int. Which results in a icompletely different number. Only in the binary representation nothing has changed

R
Rsa97, 2016-04-23
@Rsa97

*(int*)&x - take the address of the x variable and dereference it as the address of an int variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question