Get Two's complement of double

How can I calculate two's complement of c1?

 
  double c1 = (ADC_CH1[0] << 16 | ADC_CH1[1] << 8 | ADC_CH1[2]);
"Two's complement" can mean two things:

1. The binary format in which signed integral numbers are most commonly represented in computers

2. The operation that inverts the sign of a number which is represented in "two's complement" format


In the C/C++ language, this usually applies to signed integral types, such int and long.

But, floating-point numbers, such as double or float, are not integers and therefore are not represent in the "two's complement" format; they are represented in the IEEE 754 floating-point format!

TTBOMK, in the IEEE 754 format, there are separate fields for "sign", "exponent" and "mantissa". None of the fields of a floating-point (IEEE 754) number is encoded in the "two's complement" format!

This is how a float number looks in binary:
https://www.puntoflotante.net/IEEE-754-ENGLISH.jpg

(A double looks just the same in binary, just with more bits for exponent and mantissa)


So it is not clear to me what you actually want to do. Since "calculate two's complement" on a signed integer effectively inverts its sign, the equivalent operation on a float or double in C language would be:

c1 = -c1;

(which, by the way, is equivalent to a bit-flip of the "sign" bit of the IEEE 754 number)
Last edited on
Topic archived. No new replies allowed.