# Search bits in an Array of bytes

Hi, I am receiving signals in bytes. The length of the signal is not the same, so in every signal the amount of bytes is not the same. So I have an array of:

byte x = Data; // Data different in every signal
for (byte y = 1, y<=x; y++)

Data
Data
.
.
Data[y] with “y” different in every signal and between 1 to 6.

Then i must look for specific combination of bits in every byte, and i don’t know how to do it.
Please, if someone can help me ?
Thank you!

Not sure what you mean, but would bitwise AND do the trick?

Every bit pattern is also going to have a byte value, so you could test for that, too.

Could you give me an example?
I am not in a good level yet.

Do you think it will work ?

byte x = Data; // Data different in every signal
for (byte y = 1, y<=x; y++)
{
Data = Data[y]
}

{

}

That won't work because the bitRead() will always be checking Data unless that is what you want.

If you can give us a better idea of what values are being searched for, we can probably give you a better code example.

Let me give you an example:

Signal 1 : 11001001 00110110 11100000 00110011 // ( Data = 4)
// (Data = 11001001, Data = 00110110, Data = 11100000, Data = 00110011 )

then following Signal 2: 00000011 00011101 // ( Data = 2)
// (Data = 00000011, Data = 00011101)

From every byte, decoding the first three bits, bit7,6,5 show me what to do.

This is what i want to do.

I don't know how to separate these bits.

Bitwise AND with 0xe0 (aka 0b11100000)

OK. Can you explain it more, please ?

How about the plain old:

``````if(Variable==0xE0)
``````

?

Otherwise I like to do:

``````if((Variable && 0xE0) == 0xE0)
``````

Bit if the bits in between can change, and that doesn’t matter:

``````if((Variable & 0xE0) == 0xE0)
``````

Ok. I think i understand it, but how can i check it in every byte ?

``````if((Variable && 0xE0) == 0xE0)
``````

You like it all you want, but it is WRONG.
Think about it; the result of (Variable [ 0 ] && 0xE0) will be 'true' (aka one) if Variable [ 0 ] is non-zero, and zero if Variable [ 0 ] is zero.
One or zero is never, ever going to be equal to 0xE0.

Sorry, but i can't understand everything. I am trying to learn.

You mean to substitute Variable, with Data[n] and add 0xE0 (or the combination i need to check).
If it is true, i am ok.

Ok, now i understand it.

Probably one of the simplest methods for you to use, that will be easy for you to follow would be

``````byte special=data/32;
``````

Since byte type variables can't hold fractions, all the least significant bits have simply been disposed of.
You will now have a byte value that holds anything from 0 to 7 (dependent on those three bits you were interested in)

If you want the three bits separately, e.g. each as booleans

``````boolean argA = Data & 0x80 == 0x80; // 8th bit
boolean argB = Data & 0x40 == 0x40; // 7th bit
boolean argC = Data & 0x20 == 0x20; // 6th bit
``````

Each argument will be true or false depending on whether those specific bits were set in Data, so you can handle them independently.

The specific bits can be checked by using bitwise AND against the value of the single bit.

0x80 = 128
0x40 = 64
0x20 = 32
0x10 = 16
0x08 = 8
0x04 = 4
0x02 = 2
0x01 = 1

Use those values to check whether or not specific bits are set.

One more question,

Data is the number of the bytes in the signal.
If Data starts from 11 then i must decode the bytes from Data till [n-1]
If Data starts from 00 then i must decode the bytes from Data till [n-1]

I am thinking to use:

If ((Data && 0xC0) == 0xC0)
{for (n=3, n<Data,n++)

if((Data[n] && 0xE0) == 0xE0)

}else{
for (n=2, n<Data,n++)
if((Data[n] && 0xE0) == 0xE0)

Is it correct?

``````If ((Data && 0xC0) == 0xC0)
``````

No! See my earlier comment.
Single &

You want & (bitwise AND) not && (logical AND)

Also, for loops use the following syntax

``````for (initialization; condition; dec/increment)
``````

Note the use of semicolons as opposed to commas.

Yes, Yes,

You are correct, i wrote it wrong.
Single &, and ; instead of ,.