In some ADC/DAC devices their are options to output/input the data in 2's Complement form.
What are advantages of representing digital data in Two's Complement form When you can simply have straight binary code and save time of conversion?
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityIn some ADC/DAC devices their are options to output/input the data in 2's Complement form.
What are advantages of representing digital data in Two's Complement form When you can simply have straight binary code and save time of conversion?
Two's compliment representation of signed integers is easy to manipulate in hardware. For example, negation (i.e. x = -x) can be performed simply by flipping all the bits in the number and adding one. Performing the same operation in raw binary (e.g. with a sign bit) usually involves a lot more work, because you must treat certain bits in the stream as special. Same goes for addition - the add operation for negative numbers is identical to the add operation for positive numbers, so no additional logic (no pun intended) is required to handle the negative case.
While this doesn't mean it's easier from your perspective, as a consumer of this data, it does lessen the design effort and complexity of the device, thus presumably making it cheaper.
The ADC can convert data (say input voltages between 0 and 5V) and you either need that data to be unsigned (0V=0, 5V=max code) or signed (2.5V=0, 0V=max -ve, 5V=max +ve).
In addition to 2's complement being the commonest computer representation for signed data, the conversion between the two formats described above is completely trivial : simply invert the MSB!
This is incredibly cheap to add to the ADC's internal logic and gives the ADC another selling point on the datasheet...
If you need to perform math on the representations of negative numbers, twos complement makes that easier than offset binary, which will match with the "signed int" data type. Your compiler will simply know how to deal with it. Otherwise, you spend clock ticks converting back and forth.
In the question, it seems to be implied that it takes longer for the ADC to return the value in 2's complement form than in straight binary. While this might be the case in some particular implementation of an ADC, it's not true in general (for example the MSP430 series of micro-controllers have an ADC peripheral on-chip which will report the value in straight binary or 2's complement, but it takes the same number of cycles in both cases).
With that out of the way, the choice between 2's complement and straight binary mostly comes down to how your transducers work and how you like to process your data.
In straight binary mode, the ADC is giving you a number which represents the ratio between the magnitude of the analog quantity measured (virtually always voltage) and the full-scale reference quantity. For example, a 10-bit ADC can return values from 0 to 1023 (inclusive). If you measure a voltage (say, 1.25 Volts) which is half of the ADC's reference voltage (say, 2.50 Volts), the binary code you read will be half of the maximum value you could read--so, 512, or thereabouts, subject to rounding and non-linearities in the ADC.
For example, let's say you have a transducer which reports the amount of rocket fuel in a tank. 0V means the tank is empty and 2.5V Volts means it's full. So you just connect the transducer to your ADC, and away you go!
But notice that in the above paragraph, there's no way to measure negative voltages. What if we wanted to measure the flow of rocket fuel in and out of the tank (and we had a transducer to do so)? The ADC can't measure negative numbers, so we have a problem. However, there's an easy way to fake it using 2's complement mode: In this case, the transducer output is re-biased so that the zero point is halfway between the ADC's two reference voltages. In other words, positive flows are represented by voltages between 1.25V and 2.50V, and negative flows are represented by 1.25V to 0V--so flows into the tank will give ADC codes of 512 to 1023 and flows out of the thank will give codes of 511 to 0 (in straight binary format).
Now that's awfully inconvenient. We have to subtract 512 from each measurement before doing anything with it, which gives numbers in the range -512 to +511. The point of 2's complement mode is that it does this for you!
However, you still might want to use straight binary with a transducer that produces signed results. For example, your transducer might have differential outputs: In this case you'd want to subtract the inverted output from the non-inverted output anyway, so there's no advantage to using 2's complement.
The two's complement system is in use, because it stems from how simple hardware naturally operates. Think for example you car's odometer, which you have resetted to zero. Then put the gear on reverse, and drive backwards for 1 mile (Please don't do this in reality). Your odometer (if it's mechanical) will roll from 0000 to 9999. The two's complement system behaves similarly.
Please note that I'm not really offering any new information here, just the odometer example which someone might find helpful - it helped me to understand the rationale of two's complement system when I was young. After that, it was easy for me to intuitively accept that adders, subtractors etc work well with the two's complement system.
And yes, my Nissan's odometer does work this way.
Others have already answered why twos compliment binary is convenient for computer hardware. However, you seem to be asking why a A/D would output twos compliment instead of "straight binary" (whatever you think that really means).
Most A/Ds do output a simple unsigned binary value ranging from 0 to 2N-1, where N is the number of bits the A/D converts to. Note that this could also be considered twos compliment notation, just that the values never happen to be negative. If you are only dealing with positive numbers, then most binary representations are the same. This includes twos compliment and sign magnitude.
Some A/Ds, particularly those built into microcontrollers, have the option of producing negative values for the lower half of their range. This can be a perceived advantage when measuring a bipolar signal.
At least in a microcontroller, this is generally silly. In some cases you might use the A/D values directly without any conversion. In that case, you have already converted all the other values to the A/D range. Restricting that to all non-negative numbers actually simplifies things.
Otherwise, you are going to apply some scaling and offset to the A/D readings anyway to get them into whatever units you use for values internally. Here again there is no downside to unsigned values coming directly out of the A/D. In fact, I usually apply some low pass filtering on the raw values before any scaling and offset. Knowing those values will never be negative simplifies the computations slightly. Doing the scaling on unsigned values also makes things a little simpler sometimes.
I can't remember a single case in well over 100 microcontroller projects where I used the A/D in anything but unsigned output format.
So to answer the question of what the advantages are of a A/D outputting "twos compliment", there are two answers:
In dedicated hardware, there can be some advantage to have negative values indicating negative input voltage, in which case twos compliment is the easiest way to deal with the range of values. Note that if the input voltage range isn't symmetric about 0, then this advantage goes away.