LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sign Extension for 24 bit String

Solved!
Go to solution

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! Smiley Happy 

 

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

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

@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.

 

2015-05-05_fixed point.png

Message 2 of 7
(4,489 Views)

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! Smiley Happy


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.

Message 3 of 7
(4,470 Views)

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:

 

Example.png

 

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):

 

24to32_Sign_Extension.png

 

 

 

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

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:

 

24to32_Sign_Extension_using_Scale_by_Power_of_2.png

0 Kudos
Message 5 of 7
(4,452 Views)
Solution
Accepted by Leif_A

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.

Message 6 of 7
(4,440 Views)

Bravo! Works like a charm. Solution below, courtesy of nathand.

 

Sign_Extension.png

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