Yoda
Yoda

Reputation: 18068

Conversion binary floating point to hex floating point

For example I have 0 100 1110 sign exponent mantisa this is equal to 3.75. How to convert it to hexadecimal or octal form? I have searched really hard for few hours. Please help.

Could you help me here 100 is 4 1110 is 7/8 no way that's (7/8) * 2^4 is 3.75.

Upvotes: 0

Views: 12859

Answers (2)

Patashu
Patashu

Reputation: 21773

Sign should be obvious.

Mantissa means, 1 + 1/2*first bit + 1/4*second bit + 1/8*third bit... Sound familiar? It's basically binary counting, except it's 'after the decimal point'.

Exponent means, multiply the mantisa by 2^exponent. But how do you get to the bits in exponent to what value it represents? I did some googling, and apparently if the top bit is set and the rest are 0 (10, 100, 1000..., etc) then you have 1, if it's 101 you have 2, etc. Meaning if it's 011 you have 0, 010 you have -1 and so on. So in this case we have 1 for exponent.

So in this case you want to calculate 1.875 (mantisa) *2^1 (2^exponent) and make it positive (sign).

Read more: http://en.wikipedia.org/wiki/Double-precision_floating-point_format (it's for the double precision size, but all the principles should 'apply' to any size floating point)

EDIT: Here's a better explanation about the exponent.

First off, in IEEE floating points, the smallest and largest value for exponent have special meanings - all zeroes means 'this is a zero or a subnormal number', all ones means 'this is infinity or NaN'. So, even though we have three bits of exponent - 000 to 111, only 001 to 110 express normal exponents - that's only 6 values.

Now, something called 'exponent' bias is used to turn this 1...6 range into negative and positive values - in particular, you want a -2...3 range (with the largest positive exponent > the magnitude of the largest negative exponent, so you can't get overflow by doing 1/smallest normalized number. However, 1/denormalized number can produce Infinity.)

We get this by taking the value in exponent, and if it is not a special case we subtract 3 from its binary value and take this as its exponent value.

0 100 1110 is therefore

0 -> positive sign

100 -> binary value 4, subtract 3, exponent 1

1110 -> 1 + 7/8

(1+7/8)*2^1 = 3.75

Upvotes: 1

aaaaaa123456789
aaaaaa123456789

Reputation: 5842

Just convert the bits, as if it was an integer. Ignore the fact that you're dealing with a floating point number.

01001110 converts to 4E in hex, because 0100 is 4 and 1110 is E. (Notice how I group in fours.)

Same for octal, it becomes 116, because 01 is 1, 001 is 1 and 110 is 6. (Notice how I group in threes, except for the very first group.)

Upvotes: 1

Related Questions