问题描述
在阅读教程我碰到了如何在内存中重新present浮点数来了。本教程有一个浮点数的例子。
While reading a tutorial I came across how to represent Float number in memory. The tutorial had an example with a floating point number.
float a=5.2 with below Diagram
谁能告诉这5.2如何转换到二进制和它是如何重新$ P $上图中以上内存psented吗?
Can anyone please tell how this 5.2 is converted in to binary and how it is represented in memory in above the above diagram?
推荐答案
至于有人说,5.2重新psented为符号位,指数和尾数$ P $。你怎么带code 5.2?
As was said, 5.2 is represented as a sign bit, an exponent and a mantissa. How do you encode 5.2?
5很简单:
101.
剩下的,为0.2 1/5,因此通过5(或 1.00000划分
(十六进制)),你会得到 0.FFFFFFF ...
(十六进制)... 0.3333333 ...
(十六进制)。
The rest, 0.2 is 1/5, so divide 0.FFFFFFF...
(hex) by 5 (or 1.00000...
(hex)) and you get 0.3333333...
(hex).
这应该给你
0.0011001100110011001100110011...
添加5,你会得到
Add 5, and you get
101.00110011001100110011... exp 0 (5.2 * 2^0)
现在接班(标准化的话),并相应调整指数
Now shift it (normalize it) and adjust the exponent accordingly
1.010011001100110011001100110011... exp +2 (1.3 * 2^2)
现在,你只需要加127偏置(即 129 = 0b10000001
)的指数,并将其存储:
Now you only have to add the bias of 127 (i.e. 129 = 0b10000001
) to the exponent and store it:
0 10000001 1010 0110 0110 0110 0110 0110
忘记尾数的顶部1(始终应该是1,除了一些特殊值,所以它不存储),你会得到:
Forget the top 1 of the mantissa (which is always supposed to be 1, except for some special values, so it is not stored), and you get:
01000000 10100110 01100110 01100110
现在,你只需要决定很少或大端。
Now you only have to decide little or big endian.
这不正是它是如何工作的,但或多或少像时5.2一个数字转换成二进制会发生什么。
This is not exactly how it works, but that is more or less what happens when a number like 5.2 is converted to binary.
这篇关于如何重新present浮点数在内存用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!