LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

numeric data parsing

I'm processing large amounts of data to parse out data bit-wise.  Essentially I have a number that represents 16 bits and I need to get certain bits out as a number.  I've tried this two ways:
 
Convert number to boolean array
Get array subset for bits I need
Convert subset back to a number
 
This method works fine, but cant keep up on slower computers due to the amount of data to process.  I figured converting back and forth between arrays and numbers was not very efficient, so I tried using bit masks:
 
AND number with bit mask to elimiate bits i dont need
Left shift if necessary using 2**n scaling
 
This method also works fine, but is not measurably faster than the other method.  I put timers in the processing loops for both and there is no noticeable difference.
 
Is there a better way to do this?
 
TIA,
Bill F
0 Kudos
Message 1 of 4
(2,508 Views)
If you are only interested in getting one bit at a time, you don't need to do any shifting.  You can mask out the bit using AND.  If the result is greater than 0, then the retreived bit is a 1, no matter where it is located.  Example  1100 masked with 0100 produces 0100, so bit 2 is a 1, no need to shift to make 0001.
If you are getting a group of bits, then I don't see any other way to do this.  I would think that masking and shifting would be MUCH quicker that converting to an array, getting a subset, and converting back to a number.  Are you sure you are getting the right times?  Can you optimize your code elsewhere to get performance improvement?  Like a loop to mask and shift and then process the results for each data element versus a loop to mask and shift each data element and building them into an array, then another loop to process each element in the array?  Binary operations like AND and SHIFT are very quick.
- tbob

Inventor of the WORM Global
0 Kudos
Message 2 of 4
(2,503 Views)
Bill,

AND will work directly on arrays of numbers. Since you talk about loops, you presumably have your input numbers in arrays. I do not know if the 2**n function is polymorphic, but it would not surprise me that is is also.

Lynn
0 Kudos
Message 3 of 4
(2,496 Views)


AND number with bit mask to elimiate bits i dont need
Left shift if necessary using 2**n scaling

 
Are you scaling (2**n) using the math functions?  If so I would try the Logical Shift VI under [Advanced >> Data Manipulation]
 
0 Kudos
Message 4 of 4
(2,492 Views)