04-05-2012 04:55 PM
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.
04-05-2012 05:28 PM
Is there some reason why you can't just cast the number to a different representation? Or am I misunderstanding the question?
04-05-2012 06:17 PM - edited 04-05-2012 06:20 PM
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:
04-05-2012 06:33 PM
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.
04-05-2012 06:40 PM
@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.
04-05-2012 06:46 PM
It's straight forward using shifts. This gives a different output if the input is beyond 12 bits though.
04-05-2012 07:06 PM - edited 04-05-2012 07:07 PM
@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.