Casting float to int (bitwise) in C

前端 未结 7 1017
甜味超标
甜味超标 2020-11-27 21:27

Given the 32 bits that represent an IEEE 754 floating-point number, how can the number be converted to an integer, using integer or bit operations on the representation (rat

7条回答
  •  感情败类
    2020-11-27 21:27

    // With the proviso that your compiler implementation uses
    // the same number of bytes for an int as for a float:
    // example float
    float f = 1.234f;
    // get address of float, cast as pointer to int, reference
    int i = *((int *)&f);
    // get address of int, cast as pointer to float, reference
    float g = *((float *)&i);
    printf("%f %f %08x\n",f,g,i);
    

提交回复
热议问题