decimal to floating point system.

i've been asked to work on the following question with the following specification/ rules...
Numbers are held in 16 bits split from left to right as follows:
1 bit sign flag that should be set for negative numbers and otherwise clear.
7 bit exponent held in Excess 63
8 bit significand, normalised to 1.x with only the fractional part stored – as in IEEE 754
Giving your answers in hexadecimal, how would the number -18 be represented in this system?

the answer is got is: 11000011 00100000 (or C320 in hexadecimal)
using the following method:
-18 decimal is a negative number so we have the sign bit set to 1.
18 in binary would be 0010010. This we could note down as 10010. We know work on what’s on the right side of the decimal point but in this case we don’t have any decimal point or fractions so we note down 0000 0000 since there are no fractions. We now write down the binary of 18 and the remainder zeroes (which are not necessarily required) and separate them with a decimal point as shown below:
10010.00000000
We now normalise this into the form 1.x by moving the decimal point and placing it between the first and second number (counting the amount of times we move the decimal point until it reaches that area). The result now is 1.001000000000 x 2^4 and we also know that the decimal point has been moved 4 times which for now we will consider to be our exponent value. The floating point system we are using has 7 bit exponent and uses excess 63. The exponent is 4 in excess 63 which would equal to 63 + 4 = 67 and this in 7 bit binary is shown as 1000011.
The sign bit is: 1 (-ve)
Exponent is: 1000011
Significand is 00100…
The binary representation is: 11000011 00100000 (or C320 in hexadecimal)

please let me know if it's correct or if i've done it wrong and what changes could be applied. thank you guy :)

Upvotes: 0

Views: 432

Answers (1)

Kevin
Kevin

Reputation: 76254

Since you seem to have been assigned a lot of questions of this type, it may be useful to write an automated answer checker to validate your work. I've put together a quick converter in 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))

Result:

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

This confirms that -18 does convert into 0xC320.

Upvotes: 1

Related Questions