LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

twos compliment


@LV-rookie wrote:

The data that  I am receivng is hexidecimal 2's complement and what I mean by a normal hexadecimal number is a hexidecimal number without the sign bit.

 


There is really no such thing as a hexadecimal number as seen by the numeric operations within LabVIEW.  But you can have hexadecimal representations of the numeric values and those are actually ASCII characters, either in the form of strings or byte arrays of the individual decimal digits.  Before you can do anything numeric with a "hex value", you need to first convert it from its string representation into a numeric value and there are functions to do this on the string palette.

 

When you say you are receiving a 2's complement hex value it sounds like you may have an instrument that sends you serial data in the form a hex string.  You can capture such a string and convert it to either an unsigned or signed numeric value. Based on what you wrote, you want a signed conversion because you have been told that the data is in 2's complement form.  So all you need to do is tell the conversion routine that you want it to produce a signed numeric value.

 

Consider these two conversions of the same hex string, one of which uses a U16 constant as a default-value template and the other that uses an I16 constant as a template:

Clipboard04.jpg

 

When run with 0FFF, the signed and unsigned values are the same:

Clipboard05.jpg

 

But when run with AFFF, you can see the difference in the interpretation of the hex string:

Clipboard06.jpg

 

Does this get you any closer to where you are trying to go?

 

Message 11 of 19
(3,229 Views)

Yes, thank you WNM

0 Kudos
Message 12 of 19
(3,185 Views)

Thanks Altenbach-

 

If  I cannot wire an array to a conditional terminal, is there a simple way to strip off the sign bit for numbers in that array that are negative?

 

 

0 Kudos
Message 13 of 19
(3,167 Views)

Dear Rookie,

 

     Looking at the code you posted on 8 Jan, there are numerous things you don't understand about LabVIEW and about Number Representations.  In addition, you don't understand that many of us (I am a "prime offender") consider it very rude when posters show pictures of code fragments instead of posting actual code, either by attaching a VI to the post or by embedding a VI Snippet (search LabVIEW Help if you don't know what this is), which is a picture, such as the one shown here, that can be dragged into a LabVIEW Block Diagram (if the LabVIEW version is appropriate) and is magically transformed into LabVIEW code, complete with a Front Panel.  If you are running LabVIEW 2015, try it -- open a blank VI and drag the image below into the Block Diagram, where it will become code that you can execute.

I16 and U16.png

One thing you don't seem to understand is the nature of Signed and Unsigned Integers.  Here, in the top For loop, I generate all possible 16-bit integers and save them in two arrays, as I16 and as U16 values.  If you look on the Front Panel at the middle of the arrays, you'll see that the U16 goes 32765, 32766, 32767, 32768, 32769, ... while the I16 goes 32765, 32766, 32767, -32768, -32767, ... .  In particular, there is no I16 value greater than 32767.  I illustrate that just outside the For loop by testing all of the I16 numbers to see if any are greater than 32767 (you had such a test in the code you posted).  The function with the symbol for the Existential Operator is true if any of its elements are true (it does a logical OR on the array) -- you'll see (if you run the code) that it is False.  The next two tests use TypeCasts to change one representation into the other and compare the results, using the Universal Operator (which is true if all of its elements are true, doing a logical AND on the array) -- both comparisons are True.

 

So if you are dealing with numeric values, converting from I16 to U16 is a simple TypeCast operation.

 

Do you know what the name "Twos Complement" means?  A Complement is formed by inverting a bit, 0 to 1 and 1 to 0.  If you have a 16-bit word and invert all of the bits, you form something called the Ones Complement.  If you add one to it, you get the Twos Complement.  Well, it turns out that LabVIEW represents negative I16 numbers using Twos Complement representation, illustrated in the lower For loop, with the top branch being Negate and the bottom being "Complement" (or Logical Not) and Increment.

 

Finally, if you want to use an Array of Booleans to do multiple operations, there are two ways to do this.  First, simply bring the Boolean Array into a For loop using an Indexing Tunnel to transform it to a simple Boolean, and bring the array Operands into the same For loop, do the (now scalar) operation inside the loop, and bring the results back out using another Indexing tunnel.  The second way is a little less obvious (and I don't recommend it for beginners, since Code Transparency = Understanding seems, to me, more important than possible "efficiency") -- there is a Boolean operator that can change True/False to 1/0.  You can transform an array of Booleans to an array of 1's and 0's.  Multiplying by this array will "select" all of the "True" values and zero out all of the "False" values.  You can use this to build a (probably complex) arithmetic expression that will effectively "split" your array into two pieces, letting you do different things with the different pieces, recombining them by addition (since 0 + anything = anything).

 

Bob Schor

 

 

Message 14 of 19
(3,149 Views)

Thanks for the tips Bob

Yes I have a lot to learn and I will try to drag our example into my Labview but I am running  LabView 2014

so I may have to create it from scratch which can be a challenge trying to figure out what some of the symbols are...

 

Once I have this set up in my LabView 2014 I will go through your notes and I am sure that it will make more sense to me.

0 Kudos
Message 15 of 19
(3,130 Views)

@Bob_Schor wrote:

 

 In addition, you don't understand that many of us (I am a "prime offender") consider it very rude when posters show pictures of code fragments instead of posting actual code.

 


I am a prime offender of the picture posters. 😮

For various reasons (e.g. NDA ;)) I often cannot post snippets.

Especially when replying to certain simple "homework" problems, I intentionally don't post actual code, giving the student some exercise in navigating the palettes, wiring things up, and getting familiar with the LabVIEW environment. ;).

If the code is important, I attach a (often downconverted, depending on the version of the question) VI in addition to the picture. The picture allows me to remember what I was doing by just looking at the post.

 

But yes! I still consider it rude if a question contains only a picture of a code fragment for the following reasons:

 

  • If the code contains any express VIs or dynamic data, a picture is completely worthless.
  • Unless a terminal is visible, we cannot tell the difference between similar representations (e.g. U8, I16, U64 are all the same blue, SGL, CDB, etc are all the same orange)
  • We cannot see the contents of other cases (case,  stacked, and event structures)
  • We cannot tell how graphs are configured (e.g. transposed: Y/N, offset&multiplier, etc.)
  • If the image is truncated, we cannot tell where the wires come from or where they are going.
  • ... etc. etc

 

Sorry for the distraction 😄

0 Kudos
Message 16 of 19
(3,117 Views)

@LV-rookie wrote:

Thanks for the tips Bob

Yes I have a lot to learn and I will try to drag our example into my Labview but I am running  LabView 2014

so I may have to create it from scratch which can be a challenge trying to figure out what some of the symbols are...

 

Once I have this set up in my LabView 2014 I will go through your notes and I am sure that it will make more sense to me.


... and one of the things we can tell when you post code or a LabVIEW Snippet is the Version of LabVIEW you are using!  Here is my Snippet in LabVIEW 2014 -- open a new VI and drag this Snippet into it, then run it.

 

Twos Complement.png

 

Bob Schor

0 Kudos
Message 17 of 19
(3,093 Views)

Christian,

 

     I absolutely agree that those of us who are trying to help new LabVIEW users learn "how to" should not post "the complete solution", as the Original Poster will not learn very much if we "do all the work".  You might notice that I tend to be "heavy with words" in my posts -- "A Thousand Words Is Worth (More Than) A Picture".

 

     I also agree that if you are asking a question, and need help, the more information you can provide, especially information you don't realize is important or useful (because why would you be asking for help if you knew all about the problem?), the less those of us trying to be helpful have to guess (what we don't see) or spend time recreating what you show in the picture.

 

Bob Schor

0 Kudos
Message 18 of 19
(3,083 Views)

@Bob_Schor wrote:

... and one of the things we can tell when you post code or a LabVIEW Snippet is the Version of LabVIEW you are using!  Here is my Snippet in LabVIEW 2014 -- open a new VI and drag this Snippet into it, then run it.

 


I finally put my LV version in my signature because I kept asking for VIs in 2014!
0 Kudos
Message 19 of 19
(3,034 Views)