Showing results for 
Search instead for 
Did you mean: 

Finding consecutive repeating numbers in binary array (or boolean)

Go to solution

I will have a binary array or boolean array (easy to convert so either works). Ideally, the result would be 0000011111100000 and I would count the number of 1's and that's my output. That situation is really easy with a case structure inside a for loop. However, if the result is 000011110000111110000 I want to display an error since there are two chunks of 1's sandwiched between zeros (I guess could also be 001010101011 and need to display an error). Not sure how to do this part, I was thinking maybe a while loop but not sure. Using LabVIEW 2021

0 Kudos
Message 1 of 18

I think you almost "solved it yourself".  Here's a hint:  think of it as an array of Booleans, and how this might be related to the sort of Digital Signal that uses 0's and 1's, for example TTL signals, where a "High" (or 1, or "on") is conventionally 5 V, and a "Low" (or 0, or "off") is 0 V.  Look at your two (or three) example "signals" and characterize them.  How would you describe the "good" signal from, shall we say, the "noisy" signal?


Bob Schor

0 Kudos
Message 2 of 18

You can also consider converting it to a string "00000111110000011111000000" for example and looping over a RegEx match of [1]+


This will find any length of ones.... 111 or 111111111 are both found in a single loop iteration

Make sure to set the starting position after each loop.  number of matches != 1, generate your error message.

0 Kudos
Message 3 of 18

I would just count the number of rising edges (0 to 1) and if it is more than 1, you have an error.

There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 18

Assuming your "binary array" is a byte array where all 8 bits of each element counts, all you need is index into a LUT that contains the number of bits for each combination of 8 bits. Then sum the output. Make sure that the LUT datatype is sufficient to contain the final sum. (This will be very efficient. Almost no computations and 8x less memory than a boolean array.)


Edit: sorry, I misread the question, so read on ... 🙂


To ensure that you only have a consecutive stretch of ones flanked by zeroes, the LUT can be even simpler. The first nonzero value must be a sorted binary, the (optional) middle elements can only be 255, and the tail must be a reverse sorted binary. All zeroes (if any) before and after.

0 Kudos
Message 5 of 18

Hi simmer,


read up on "Run Len Encoding" (aka RLE)…

Best regards,

using LV2016/2019/2021 on Win10/11+cRIO
0 Kudos
Message 6 of 18

Thank you everyone! I wasn't sure if there was an easier way with a search pattern vi or similar, but it seems like nested structures and a counter is the way to go. Thanks!

0 Kudos
Message 7 of 18

It would really help to define the problem better. What exactly is a "binary array" vs. a "boolean array"? How long can it possibly be? Is the length in e.g. integer multiples of eight?


What is the desired output? I would suggest a I32 count of the consecutive ones (a nonnegative number: 0..N) and -1 for an error. (unless zero ones is also an error?).


Done right, the code can be very simple.



0 Kudos
Message 8 of 18

@simmertime wrote:

Thank you everyone! I wasn't sure if there was an easier way with a search pattern vi or similar, but it seems like nested structures and a counter is the way to go. Thanks!

I don't think you need any structures or counters.


Here's one possibility:




0 Kudos
Message 9 of 18

The input is a digital waveform of size 496 (1D). I convert it to a boolean array(T/F) and binary array(0/1) for other reasons. From the input I want to count how many ones are sandwiched between zeros, but if there are more than 1 sandwich I want to have a string indicator that there is an error. If there are no 1's found, I would like the string indicator to say none found. 

000111111000 Count= 6

0001110000111100 "Error"

000000000000 "None found" 

0 Kudos
Message 10 of 18