Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 ;)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this