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)
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.
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.
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.