LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract specific bits from a true binary string

So you really need to get 28 consecutive bits out of a series of 4 bytes. You can do a boolean end of your 4byte number with a U32 constant with those 28 bits of interest set as 1 and the rest as 0. (2^28 -1) You mentioned that the data is big endian. So it sounds like you may or may not need to swap either bytes or words. If so, there are swap bytes and swap word functions on numeric data manipulation palette. If those 4 bytes of interest are part of a larger byte array, then you will want to use Array Subset to extract out the 4 particular bytes you want. Another function you should look up is TypeCast.
Message 11 of 15
(1,910 Views)
Did you try the bitwise and operation I suggested, this should be the fastest method, it is used in embedded and microcontrollers often since each work tested requires only 2 operations a and then an equal to zero compare.  You can possible use labviews compiler and do chunks of ands and compares in array (a string is just an aray) which tend to be faster (the compilier might use extension commands sets to do multiple operations in parallel but just know from experuance that processing arrays is fasetr then individule elements inside of a for loop. 
Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 12 of 15
(1,881 Views)

Here is a start.  I assumed that the string are in 32 bit words from your source, no endian assumptions you could do a byte swap is required.

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 13 of 15
(1,877 Views)

If I understand your request correctly I've had to do something very similar. (My solution is VERY different to Paul's, we seem to have made different assumptions about the sort of data you want returned)

 

With my solution I started with the assumption that converting to a binary array and manipulating it would actually take longer because LabVIEW isn't very quick with array manipulations.

 

So I try to do as much as possible with math/bitwise operations on integers rather than Boolean arrays.

 

Because I am interested to see the performance differences of the two approaches (and possibly get feed back from some other experts here as to how to simplify / improve the code), I modified my solution to suit your question and then coded another version which produces the same result but using Boolean arrays. (Use "Type" enum to select between the two.)

 

Results:

Math-bitwise vs. Boolean array... run 1million times and see how long it takes...math-bitwise wins, (in my example it's approx. 20 times faster.) Of course I haven't spent a lot of time on it and tested it thoroughly. See attached file.

 

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
Message 14 of 15
(1,855 Views)

Thank you all for the help. I ended up going a slightly different route using bitwise math. But, I appreciate all the ideas, food for thought on different projects.

 

Josh

0 Kudos
Message 15 of 15
(1,836 Views)