【问题标题】:decimal to floating point system.十进制转浮点系统。
【发布时间】:2014-01-29 19:08:59
【问题描述】:

我被要求使用以下规范/规则来解决以下问题...
数字保存在 16 位中,从左到右拆分如下:
应为负数设置的 1 位符号标志,否则清除。
Excess 63 中保存的 7 位指数
8 位有效数,归一化为 1.x,仅存储小数部分——如 IEEE 754
以十六进制给出你的答案,数字 -18 在这个系统中如何表示?

得到的答案是:11000011 00100000(或十六进制的C320)
使用以下方法:
-18 十进制是负数,因此我们将符号位设置为 1。
二进制中的 18 将是 0010010。我们可以记下它为 10010。我们知道在小数点右侧的工作,但在这种情况下,我们没有任何小数点或分数,所以我们记下 0000 0000,因为那里不是分数。我们现在记下二进制的 18 和余数为零(这不是必须的),并用小数点分隔它们,如下所示:
10010.00000000
我们现在通过移动小数点并将其放在第一个和第二个数字之间(计算我们移动小数点直到它到达该区域的次数)将其标准化为 1.x 形式。现在的结果是 1.001000000000 x 2^4,我们还知道小数点已经移动了 4 次,现在我们将其视为指数值。我们使用的浮点系统有 7 位指数,使用 63 余数。指数是 4 超过 63,等于 63 + 4 = 67,这在 7 位二进制中显示为 1000011。
符号位为:1 (-ve)
指数为:1000011
有效数字是 00100…
二进制表示为:11000011 00100000(或十六进制的C320)

请让我知道它是否正确或我做错了什么以及可以应用哪些更改。谢谢你:)

【问题讨论】:

  • 这看起来与您之前的问题之一非常相似stackoverflow.com/questions/21029217/…
  • 不知道为什么有人会否决这个问题。我只是想确认我的回答是否正确,实际上是正确的。不需要消极。

标签: math floating-point decimal computer-science hex


【解决方案1】:

由于您似乎被分配了很多此类问题,因此编写一个自动答案检查器来验证您的工作可能会很有用。我在 Python 中组装了一个快速转换器:

def convert_from_system(x):

    #retrieve the first eight bits, and add a ninth bit to the left. This bit is the 1 in "1.x".
    significand = (x & 0b11111111) | 0b100000000
    #retrieve the next seven bits
    exponent = (x >> 8) & 0b1111111
    #retrieve the final bit, and determine the sign
    sign = -1 if x >> 15 else 1

    #add the excess exponent
    exponent = exponent - 63

    #multiply the significand by 2^8 to turn it from 1.xxxxxxxx into 1xxxxxxxx, then divide by 2^exponent to get back the decimal value.
    result = sign * (significand / float(2**(8-exponent)))
    return result

for value in [0x4268, 0xC320]:
    print "The decimal value of {} is {}".format(hex(value), convert_from_system(value))

结果:

The decimal value of 0x4268 is 11.25
The decimal value of 0xc320 is -18.0

这证实 -18 确实转换为 0xC320。

【讨论】:

    猜你喜欢
    • 2015-10-11
    • 2016-01-11
    • 1970-01-01
    • 2021-09-10
    • 2014-03-01
    • 2016-06-02
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多