Casting float to int (bitwise) in C

前端 未结 7 1019
甜味超标
甜味超标 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:50

    C has the "union" to handle this type of view of data:

    typedef union {
      int i;
      float f;
     } u;
     u u1;
     u1.f = 45.6789;
     /* now u1.i refers to the int version of the float */
     printf("%d",u1.i);
    
    0 讨论(0)
提交回复
热议问题