ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert C code in LabVIEW

Hi there I have a quicik question.  i have a line of C code that I am trying to implement in LabVIEW and I was wondering if someone could show me how to do so.  The following is the line of code that i am trying to implement:

 

if ((temp = temp1 & (unsigned) 0xFFF) & 800)

       temp = temp - 4096

 

Where temp is 16 bit signed and temp1 is 16 bit unsigned.

This line of code is meant to perform a sign extension.  temp1 is a 16 bit unsigned number.  I am then performing logic AND with FFF to mask the first four upper most bits.  Then I am going to check to see if the upper most bit is  a one.  If it is then i am going to subtract 4096 from this number. 

 

If someone could send me a snippet of code that could implement this line, it would be greatly appreciated.

0 Kudos
Message 1 of 7
(4,250 Views)

Is there some reason why you can't just cast the number to a different representation? Or am I misunderstanding the question?

0 Kudos
Message 2 of 7
(4,246 Views)

Let me see if I understand correctly: you have a 12-bit signed value, stored in a 16-bit value, and you want it sign-extended through 16 bits?  I think there are easier ways to do this in both LabVIEW and C.  Here's one idea:

sign-extend 12 bits to 16.png

Message 3 of 7
(4,231 Views)

I think you are right with the 12bit assumption and your solution.

However, I would prefer the numeric way and thus avoid the BD constants.

 

U12toI12.png

0 Kudos
Message 4 of 7
(4,217 Views)

@GuenterMueller wrote:

I think you are right with the 12bit assumption and your solution.

However, I would prefer the numeric way and thus avoid the BD constants.


Your method does not produce the right result.  A decimal value of 4095 (-1 for a 12-bit value, 0xFFF) correctly returns -1 in my code, but returns -4095 in yours.

0 Kudos
Message 5 of 7
(4,201 Views)

It's straight forward using shifts. This gives a different output if the input is beyond 12 bits though.

Sign Extension.png

Message 6 of 7
(4,191 Views)

@nathand wrote:

Your method does not produce the right result.  [...]


Thanks. Your are right. My approach does not consider that the numbers still are ordered in a ring.

0 Kudos
Message 7 of 7
(4,178 Views)