One's Complement and Two's Complement of a Binary Number in LabVIEW
Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 7.1
Primary Software Fixed Version: N/A
Secondary Software: N/A
What are the one's complement and two's complement of a binary number? How do I perform a two's complement in LabVIEW?
LabVIEW makes it very easy to perform the two's complement. Simply pass the integer number through the NOT function (which gives the one's complement by applying the NOT operator bitwise on the integer) and then add one to this result. There is no need to convert the number to a Boolean array. Note that the integer representation of both the source value and the result value have to be a Signed Integer (I8. I16, I32 or I64) in order to use the NOT operator.
A one's complement and a two's complement are ways of representing negative numbers in binary. To form the one's complement of a number, simply invert each bit. To form the two's complement, add 1 to the one's complement:
decimal value: 9
binary value: 01001
one's complement: 10110
two's complement: 10111
decimal value: -9
The advantage to using two's complement to represent the negative number is that the two's complement can be used in additions and subtraction with no need to use a bit for the sign of the value. For example, if we want to perform the decimal operation:
5 + (-9) = -4We can perform the following binary addition:
Note that this result is the two's complement representation of -4, which is the answer to the original decimal addition.
The VI below shows a simple example of calculating the two's complement representation of a signed integer.
Labview 2009 Help: Numeric Data Types Table
Wikipedia: Two's Complement
Report Date: 07/22/1998
Last Updated: 04/10/2013
Document ID: 1BLC7RU0