Convert Signed Float to binary 2s complement

Hi

I would like to know how convert signed float to 16bit format in 2's complement.

Let me explain better I am communicating with ADS131M08 and I need write offset in 2s complement.
EX: -0.000253 or +0.000253
Can someone tell me how to convert positive or negative float to 2s complement?(in Code)

format

Thanks

That would NOT have a decimal, so first multiply the float by a constant to get rid of the decimal value.

Do you mean an integer value? If not, please explain what you mean by "16bit binary".

EX2: 0.12

This is made of 0 Units, 1 tenth and 2 hundredths

Multiply the fraction by the base (2), keep the product unit, repeat.
0.12 * 2 = 0.42 (keep 0)
0.42 * 2 = 1.64 (keep 1)
0.64 * 2 = 1.28 (keep 1)
0.28 * 2 = 0.56 (keep 0)
0.56 * 2 = 1.12 (keep 1)
0.12 * 2 = ... repeating, so stop
Result is (0.12) 2 = 0.01101

Two's compliment is: invert binary and add 1
00000000000.01101 >> 11111111111.10011

Are you quite sure about the above calculations?

Are you trying to describe some sort of fixed point representation to the OP? If so, it might be better to point the person to conventional documentation for the particular fixed point notation you have in mind.

Example tutorial
on fixed point representations.

I personally have no idea what the OP is trying to do.

[edit]... ah, oops... I see .42 * 2 should be closer to .84... eek... and same with .12

No, I am not. The example was from the OP post. I do not know "fixed point representations."

I remember two's compliment (invert and add 1).
I remember multiplying the "binary decimal" by the base, recording the carry, repeat.
I also remember another method of "adding binary decimals" like ".1" = .5 , .01 = .25, ... but could not get the whole thought out.

The OP needs to read the tutorial (or something similar) and decide what they want to do.

There is really nothing to the conversion to fixed point. The first step in fixed point notation is to decide where the fictitious decimal point should be, to balance the integer range with the precision.

Example 16 bit unsigned, format 11.5 == 11 bits for the integer part, 5 bits for the fraction.

To convert a positive float value to 11.5 notation, multiply by 2^5 = 32 and take the integer part.

Example 1.5 x 32 = 48, or binary 110000

The fictitious decimal point in 11.5 is between the two 1s: 1.10000

Example 1.25 x 32 = 40, or binary 101000 (1.01000 if you like)

To recover the floating point number, divide by 32. For the first case, 48/32.0 = 1.5.

Dealing with sign is a separate issue, and there are three different approaches.

@Shahrezau please do not edit your original post. It make the thread difficult or impossible to follow.

You have not yet provided enough information to answer the question. Ask the manufacturer of the gizmo, or read the data sheet/user manual more carefully.

@jremington

I apologies if I confuse you. I like you comment ask manufacture.
I already read the Data sheet and I could not find how to convert float to 2's complement, otherwise I never asked the question.
Thanks again for your help.

You need to find out how the manufacturer defines "2's complement binary".

We certainly can't know, as you haven't told us what the device is or who made it.

@Shahrezau

In ATmega328P architecture, negative integer numbers are treated in 2's complement form.

The negative/positive floating point numbers are treated in binary32 format (aka IEEE-754 standard) in which the binary representation is a 32-bit number.

What is your requirement?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.