我正在编写的代码在某些时候需要从float
转换为uint8_t
。很酷的部分是,浮动已经具有与uint8_t
相同的值,例如15.0
=> 15
或4.0
=> 4
。
我现在需要编写一个将32-bit float
转换为32-bit unsigned integer
的函数,以使float和整数的值相同。因此,只能设置整数的第一个4 bits
。
以下是我正在使用的内容:
Binary representation for: 0.0 00000000000000000000000000000000 => 0000
Binary representation for: 1.0 00011111110000000000000000000000 => 0001
Binary representation for: 2.0 00100000000000000000000000000000 => 0010
Binary representation for: 3.0 00100000001000000000000000000000 => 0011
Binary representation for: 4.0 00100000010000000000000000000000 => 0100
Binary representation for: 5.0 00100000010100000000000000000000 => 0101
Binary representation for: 6.0 00100000011000000000000000000000 => 0110
Binary representation for: 7.0 00100000011100000000000000000000 => 0111
Binary representation for: 8.0 00100000100000000000000000000000 => ....
Binary representation for: 9.0 00100000100010000000000000000000
Binary representation for: 10.0 00100000100100000000000000000000
Binary representation for: 11.0 00100000100110000000000000000000
Binary representation for: 12.0 00100000101000000000000000000000
Binary representation for: 13.0 00100000101010000000000000000000
Binary representation for: 14.0 00100000101100000000000000000000
Binary representation for: 15.0 00100000101110000000000000000000