# Convert Signed fixed-point bit value to 32bit float?

This topic is 4247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All, I'm working on a bit of code parsing, and I'm reading in a 22bit value, the data is bit aligned...bit messy....and the value I'm reading in is Signed fixed-point bit value It says in the docs : The n bits are the lower n bits of a FIXED value. Just assign them to a 32 bit unsigned int value, and use the result as a FIXED. (LS 99.06.23) But I can't seem to convert it to a 32bit float? Any ideas etc would be great. Thanks, Ben

##### Share on other sites
This depends on the fixed-point format used. I'll guess randomly that a FIXED is in the 1.15.16 format, ie. one sign bit, 15 integer-part bits, and 16 decimal-part bits. (This is a typical arrangement.)

In that case, what you want to do is convert the whole thing to float, and then divide by 0x10000, or 65536, which will account for the 16 decimal bits: float result = ((static_cast<int>(fixedval)) / 65536.0);

If the FIXED format is different, you can calculate the divide offset easily as 2(num decimal bits)

Hope that's enough to get the job done [smile]

##### Share on other sites
A fixed-point value is simply an integral one that is interpreted as being 1/2^n times what the interpretation as an integral value would be (for some n).

Normally they have the same format as built-in integral values do on your system, with something like 'FIXED' simply being a typedef, so that you can just cast and divide as shown. But ApochPiQ, I don't think you really mean "1 sign bit" - ints are normally 2s-complement, and static_cast between two integral types won't cause a conversion of that sort ;)

1. 1
Rutin
21
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×