Casting float to int (bitwise) in C

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

    You cannot (meaningfully) convert a floating point number into an 'integer' (signed int or int) in this way.

    It may end up having the integer type, but it's actually just an index into the encoding space of IEEE754, not a meaningful value in itself.

    You might argue that an unsigned int serves dual purpose as a bit pattern and an integer value, but int does not.


    Also there are platform issues with bit manipulation of signed ints.

提交回复
热议问题