10-12-2012 10:14 AM - edited 10-12-2012 10:15 AM
I have 3 U8 bytes in an array (a byte received 24bit number). How can I convert them into a 2's compliment I32 number for general use?
Solved! Go to Solution.
10-12-2012 10:43 AM
First of all, there are 4 bytes in an I32. So what do we do with the extra byte? Where does this extra byte go?
Look into the Join Numbers in the Numeric->Data Manipulation palette.
10-12-2012 11:16 AM - edited 10-12-2012 11:23 AM
Still a bit lost. I am planning on using the "Type Cast" function to change a byte (U8) array of length 3 into an I32. It has the useful feature that it places the bytes from the array in the upper part of the created I32. It means, that if I had an Arithmetic Shift operation, I could propogate the MSBit (bit 31) down to bit 23, thereby preserving the 2's compliment nature of the value. So a negative 24bit number is the same as in a 32bit number.
My problem is, I am not sure whether there is a better Shift function than the "Shift Right With Carry" function?! If I use that, it means I need to expand the number to a Boolean array, just to get at Bit31 to set as the CarryIn of the "Shift-Right With Carry" function. It seems over the top to me.
Anything better available?
Thanks.
10-12-2012 11:23 AM
What's wrong with the Logical Shift function?
10-12-2012 11:27 AM
It does not preserve the state of the MSBit. Thereby losing the 2's compliment nature of the number when you shift it down to its proper position in the 32bit value.
Its the 2's compliment aspect of this which I am wrestling with. In C text-based programs its easy. I am looking for the equivalent in LabVIEW.
10-12-2012 11:47 AM - edited 10-12-2012 11:47 AM
How would you do it in C?
Here's what I'm guessing you are looking for. Still not entirely sure if I got it yet.
10-12-2012 11:58 AM - edited 10-12-2012 11:59 AM
You can also do it using the Select function, if you prefer less logic functions. Same skin off the cat.
As you can see, LabVIEW is just as capable as C at bit-twiddling.
10-12-2012 12:07 PM
For what its worth, I got this to work. But like you, would like to avoid arrays. Is there no simple way of getting a single bit (boolean) from a numeric?
10-12-2012 12:10 PM
Wow, that's .... excessive. You should look at the two examples posted. LabVIEW is no different than C in terms of numerics. In C, you don't have bits, but bytes. If you want to know if a certain bit is 1 or 0 you AND the numeric with an appropriate mask, and check if the resultant numeric is 1 or 0. Same thing in LabVIEW.
10-12-2012 12:25 PM
I wanted to make a .vi so I could get a 2's complement value from the contents at any position in a U8 array. My problem being that I have a byte stream which is feeding encoded values that can be 32 bits, 24 bits or 16 bits in size. I would like to be able to somehow take the 'type' parameter out to make it really general purpose. Do you know of any vi's that already do all this?
I may even have to deal with odd size values like 10bits!
Thanks very much for the simplified solution. I may just go with simpler bespoke code for each extraction type I need to make.
I am beginning to see the parallels with C. Though I still think an Arithmetic Shift would be useful. Especially as the Type-Cast operator conveniently positions the value it reads from its input, at the top end of the 32bit value.
Thanks again.