05-05-2015 10:59 AM
Hey LabVIEW gurus,
I am trying to sign extend a 24 bit string to an I32. For example, F0F0F0 (111100001111000011110000) would need an extra byte of 1's added to the left to produce FFF0F0F0 (11111111111100001111000011110000) and 333333 (001100110011001100110011) would become (00000000001100110011001100110011). I am surprised there aren't more examples of sign extension in this forum, but perhaps that's because it is too simple!
I think I found a good, relevant example here, but I can't figure out how to recreate the gray constant - I don't know what the gray constant/wiring means. I am using LabVIEW 2012, so I am not able to drop it in my block diagram. Although I doubt it will be helpful, I attached a .vi with my various attempts, most of which are based on code I found in the forum. Thank you in advance for your time and direction! It is much appreciated.
Thank you,
Leif
Solved! Go to Solution.
05-05-2015 11:40 AM
@Leif_A wrote:
Hey LabVIEW gurus,
I think I found a good, relevant example here, but I can't figure out how to recreate the gray constant - I don't know what the gray constant/wiring means.
it's a fixed point constant.
05-05-2015 12:02 PM
Leif_A wrote:
I am surprised there aren't more examples of sign extension in this forum, but perhaps that's because it is too simple!
The function you want is "Scale by Power of 2" which is essentially a bit shift with sign extension. If your 24 bits of signed data are in the upper bits of a 32-bit value, and you scale by 2^-8, you'll have shifted your data down 8 bits and filled the upper bits with the sign.
05-05-2015 12:19 PM
Thanks alexderjuengere,
I was able to change to the floating point constant and then experimented with the code. Unfortunately, I couldn't get this to work. Here it is in 2012 if anyone is interested:
However, I developed a different solution that seems to complete my sign extension correctly. It might be a little clunky, but I think it works. If you want to use a different string length, just edit the minimum field width of the control (named 24-bit value), change the 32-bit constant to have the same number of zeros as the total length of your string (for example: if your string is 20 bits long, the constant should have twelve 1's followed by twenty 0's "11111111111100000000000000000000"), and change the index constant of the Index Array function to be the element of your MSB (for example: it should be 19 if your string is 20 bits):
05-05-2015 12:33 PM
Hi nathand,
Thanks for your reply! I tried what you suggested, but didn't get what I am looking for. I am not using the upper 24 bits of a 32-bit value, I am using the lower 24. Here's what I did:
05-05-2015 01:00 PM
Almost there. If you're starting with the data in the lower bits, first multiply by 2^8 (to get the data in the upper 24 bits), then by 2^-8.
05-05-2015 01:07 PM
Bravo! Works like a charm. Solution below, courtesy of nathand.