Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
399 ratings:
 2.90 out of 5     Rate this Document

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

Problem:
What are the one's complement and two's complement of a binary number? How do I perform a two's complement in LabVIEW?

Solution:
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:

Example:

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) = -4
We can perform the following binary addition:
	  00101
+ 10111
-------
11100

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.



Related Links:
Labview 2009 Help: Numeric Data Types Table
Wikipedia: Two's Complement

Attachments:
Simple Example For Getting a Two's Complement of a Numbertwos_complement.vi




Report Date: 07/22/1998
Last Updated: 04/10/2013
Document ID: 1BLC7RU0

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit