32位IEEE 754单精度浮点数到十六进制 [英] 32-bit IEEE 754 single precision floating point to hexadecimal
问题描述
我已经学习了如何将数字转换为浮点数(在二进制,八进制和十六进制之上),并且知道如何将数字转换为浮点数.
I have learnt how to convert numbers to floating point (on top of binary, octal and hexadecimal), and know how to convert numbers to floating point.
但是,在浏览工作表时,遇到了以下问题:
However, while looking through a worksheet I have been given, I have encountered the following question:
使用32位IEEE 754单精度浮点以十六进制表示-12.13.
我尝试查看自己拥有的资源,但仍然不知道如何回答上述问题.给出的答案是 0xc142147b .
I have tried looking at the resources I have and still can't figure out how to answer the above. The answer given is 0xc142147b.
对不起,您不清楚,但我想知道如何手动完成此工作,而不是对其进行编码.
Sorry for not clarifying but I wanted to know how to get this done by hand instead of coding it.
推荐答案
-12.13
必须先转换为二进制,然后再转换为十六进制.让我们或多或少像 glibc 库那样做,只使用笔和纸以及Windows计算器.
-12.13
must be converted to binary and then hex. Let's do that more or less like the glibc library does it, using just pen and paper and the Windows calculator.
删除标志,但请记住我们有一个标志:12.13
Remove the sign, but remember we had one: 12.13
整数部分12
很简单:C
(十六进制)
The integer part, 12
is easy: C
(hex)
小数部分0.13
有点棘手. 0.13
是13/100
.我使用Windows计算器(程序员模式,十六进制),并将13
(十六进制D
)左移32(*)位:D00000000
.用100
(十六进制64
)除以得到:2147AE14
十六进制.
The fractional part, 0.13
is a little trickier. 0.13
is 13/100
. I use the Windows calculator (Programmer mode, hex) and shift 13
(hex D
) by 32(*) bits to the left: D00000000
. Divide that by 100
(hex 64
) to get: 2147AE14
hex.
由于我们需要一个小于1的值,因此我们再次右移32位,并得到:0.2147AE14
Since we need a value below 1, we shift right by 32 bits again, and get: 0.2147AE14
现在在左侧添加整数部分:C.2147AE14
Now add the integer part on the left: C.2147AE14
我们只需要24位尾数,所以我们舍入:C.2147B
-> C2147B
We only need 24 bits for the mantissa, so we round: C.2147B
--> C2147B
现在必须对此进行归一化,因此将二进制点向左移动3位(当然,这些位保持不变).指数(最初为0)相应地提高了3,所以现在是3.
Now this must be normalized, so the binary point is moved 3 bits to the left (but the bits remain the same, of course). The exponent (originally 0) is raised accordingly, by 3, so now it is 3.
现在可以删除隐藏的位:42147B
(现在是23个低位)
The hidden bit can now be removed: 42147B
(now the 23 low bits)
现在可以将其转换为32位值:0x0042147B
This can be turned into a 32 bit value for now: 0x0042147B
现在让我们接受指数:3
+十六进制7F
的偏差=十六进制82
或1000 0010
二进制.
Now let's take on the exponent: 3
+ bias of hex 7F
= hex 82
, or 1000 0010
binary.
在左侧添加符号位:1 1000 0010
.重新组合:1100 0001 0
或C10
Add the sign bit on the left: 1 1000 0010
. Regrouped: 1100 0001 0
or C10
当然,这些是最高位,所以我们将完整的32位变成0xC1000000
Of course these are top bits, so we turn that into 0xC1000000
for the full 32 bits
0xC100000 | 0x0042147B = 0xC142147B
这就是您想要的值.
(*) 32位,所以以后有足够的位数可以正确取整.
这篇关于32位IEEE 754单精度浮点数到十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!