Page 1 of 1

Find first 0-bit in an array

Posted: Mon Oct 19, 2020 11:33 pm
by MJU
I have a variable array, let's say data[20].
These are filled with data.
In this array I need to find the first 0 byte.

So if the array looks like
data[1] = 11111111
data[2] = 11111111
data[3] = 11010101
data[4] = 11010101
data[5] = 11110111
.....
This has to return the 2nd bit of the 3th byte.

The first byte '(data[1]) contains data from 7 to 0.
The second byte (data[2]) data from 15 - 8
So I need to find the 2nd bit in the 3th byte.
And this needs to get converted to "17" because it's the 17th spot in the data array.

What I first did was to loop through the array until an data byte <>255
So this loop stops at data[3].
Then I check bit per bit if it is 1 or 0 through a loop.

Code: Select all

 Loop until Zerobit = 0 
 ZeroBit = (data[3] >> Counter) & 1
 counter = counter + 1
 End loop
So in my opinion the counter that checks the bits of the byte must stop at position 2.

Is there a more lean way of checking an array of bytes for the first 0 value?
What is the easiest method to find the first 0 in this array and return the spot of where it resides?

Thanks

Re: Find first 0-bit in an array

Posted: Tue Oct 20, 2020 7:25 am
by mnf
Looked fun - so I had a go at a general solution...

Here I assume you want 0..7 8..15 16..23 (as byte 0, 1, 2) as the bit positions and I return the bit position in the array - if no zero bit then return number of bits in array (24 here)

So with your example it would return 17 (3 byte 2 bit)
FirstZero.fcfx
(12.19 KiB) Downloaded 149 times
Note - it won't simulate as it stands - I use a line of C to get the length of the array. Use a calculation to set .length to test in the simulation if needed.

Martin