LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract specific bits from a true binary string

I am creating an app that will recieve a string of true binary data (not ascii binary) and want to extract a certain number of bits( 1 bit, 27 bits, 11 bits, etc.) from it at any location at a given time. Any suggestions. I can't send my code due to the job restrictions.

 

Josh

0 Kudos
Message 1 of 15
(11,220 Views)

Typically you will BITWISE AND a binary number with a binary number where the number has all zeros except the test bit then see if the number is greater than zero, if yes the the bit was set. 

 ie for a 16 bit number, to test the 11th bit you would and the original number with 0000010000000000 (this is the decimal number 2^11 or 2048).  In labview you also can use the number to boolean array and index the array by the bit index and see if it is true or false.

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

What do you mean by 'true binary data'? What LabVIEW data type is your input?

If you're extracting a number of bits from the middle of a large number and you do a bitwise AND to extract the part you want, you may also need to bit shift the data so to get the right result.

 

Eg. AND 010110010

   With 000110000 will give you 48, but the answer you may want is 3 because you only want the value of a 2 bit number.

 

Perhaps you could give an example table of 'data in - data expected out', including LabVIEW data types.

Eg.

Data in(U8 array) BitOffset(U32) BitLength (U32)    Result(U32)

0xBEEF            5              3                  7

0xABBA            4              4                  11

0xDEAD            13             2                  2

Message Edited by Troy K on 04-17-2009 10:38 AM
Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 3 of 15
(11,215 Views)
Troy I want to be able to extract bits in the manner you have suggested, but in the most efficient and fastest way possible. Speed is a big issue here.
0 Kudos
Message 4 of 15
(11,197 Views)

How long is your bit stream? (How long is a peice of string?...What is the maximum you expect it to be?)

Is it an array of U8s? A sample of real[istic] data would be useful.

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 5 of 15
(11,194 Views)
0300 4757 2044 2020 here is a sample in big endian. It is an array of U8 and there is no size limit. I get an array of variable length with the desired bits starting at a variable position in the binary and the number of desired bits vary.
0 Kudos
Message 6 of 15
(11,192 Views)
If you have an array of U8,  you are essentially dealing with bytes.  Do you actually mean bytes rather than bits?  Is the example you posted a hex value?
0 Kudos
Message 7 of 15
(11,189 Views)

What data type do you want as your output? Integer? Array of U8s? Boolean Array?

 

I always try and avoid a lot of array manipulation, it tends to be slow. And storing data as a boolean array is inefficient too. I believe each bit is stored as a byte anyway. 

 

With a U8 array as input, I would start by getting an array subset starting at 'BitOffset' modulo 8 (BitOffset%8) and array length of BitLength modulo 8 +1...or something like that.

This reduces the size of the array you're working with first.

 

What you do next depends on the data type you want out. 

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 8 of 15
(11,184 Views)
I mean bits. the values are hex representation of the binary string. If I'm asked to yank out 1 word from the array and need the value of bits 2 to 3 of the that word. Just keep in mind, i might not always be given a word to yank out. I might have to yank out only 27 bits out of the string and examine it as a 27 bit signed integer. How I examine the bits will vary. For right now, I just want to yank out x number of bits from any point in the string.
0 Kudos
Message 9 of 15
(11,183 Views)
I have no choice about the nature of the binary string. I'm dealing with legacy data.
0 Kudos
Message 10 of 15
(11,180 Views)