LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

2's complement

I like to convert 0xFFFF ~ 0x8000 to -1~-32767 by use 2's complement method.
I was not able to find in the online help, could someone tell me:
1. How to do the 2's complement with CVI?
2. How to find these kind of information in CVI help?
 
Thanks a lot
0 Kudos
Message 1 of 5
(4,131 Views)
To get the the twos compliment of a number in C, multiply the number by -1.
 
CVI does not provide a great deal of information on the ANSI C programming language beyond the function panels.  But documentation on the C language is readily available on the web.  Use google or your favorite search engine.
Good Luck
0 Kudos
Message 2 of 5
(4,121 Views)
Hello,

Signed integers are already represented using two's complement, so if you are only concerned with 16-bit integers (like 0xFFFF and 0x8000), all you have to do is cast the value as a short int. If you need to interpret other sizes, the general method would be this:

int highBitMask = 1 << (numBits - 1);

(~highBitMask & number) - (highBitMask & number);

(This assumes that number is a value who's two's complement value is within the range of [-2^numBits, 2^numBits  - 1].  That is, no bits above numBits are set.)

As far as finding help, you can search for specific topics in the CVI help by going to Help->Contents then typing keywords into the Index or Search tabs, but for things like data representaion or C language specifics, you're probably more likely to find what you're looking for in a good reference book or by searching the web.

Hope this helps!

Mert A.
National Instruments
   

Message 3 of 5
(4,118 Views)

If I remember my maths correctly (its been a while...) you obtain the 2's complement of a number by first taking the 1's complement and then adding 1. So, in C:

int number;

int complement_2;

complement_2 = ~number + 1;

JR

0 Kudos
Message 4 of 5
(4,076 Views)

Thanks for all your replys, after 5 years not touching C & CVI, I'm rusted... Your helps really put me back to programming mode. Thanks.

FerroK

0 Kudos
Message 5 of 5
(4,068 Views)