2014-01-10 54 views
-1

我被要求使用以下规范/规则处理以下问题...
数字保持16位,从左到右分割如下:
1位符号标志,应设置为负数数字和其他明确。超过63
8位有效,只有小数部分举行,标准化为1.X
7位指数存储 - 在IEEE 754
给予十六进制的答案,如何将数量-18在这个系统代表?十进制浮点系统。

的回答是得到的是:11000011 00100000使用下面的方法(或C320以十六进制)

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

请让我知道这是否是正确的,或者如果我已经做到了错误的以及可以应用哪些更改。谢谢你的家伙:)

+1

这看起来非常类似于您以前的问题之一http://stackoverflow.com/questions/21029217/decimal-to-floating-point-conversion-using-16-bit。 –

+0

不知道为什么有人会投下这个问题。我只是试图确认我的回答是否正确,实际上是这样。没有必要是消极的。 – ComputerScienceStudent

回答

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。