2014-01-10 98 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。