Comparing part of a Byte

Hey guys,

I'm trying to compare the first number of a byte, instead of checking the whole byte to reduce code.
The situation is as follow:

I'm comparing an incoming message (byte array) to another byte array.
I need to know if the first number in the 4th byte is a 2. It doesn't matter wat the next number in that byte is.
Usually the byte will be 0x20, 0x21, 0x22 or 0x23

Is there a cleaner way to check this, rather than:

byte indicatorDirection = packet[4];

 if (indicatorDirection == 0x20 || indicatorDirection == 0x21 || indicatorDirection == 0x22 || indicatorDirection == 0x23) {

}

I've been thinking I could also just work with it as a decimal, since the value would then always be between 32 and 35, but I just wanted to know if it's possible to 'extract' the first part of the byte.

all help appreciated!

If ((IndicatorDirection & 0xF0)==0x20)

Bitwise and with 0xF0 to mask off the low half of the byte.

Or

If ((IndicatorDirection >> 4)==0x02)

Rightshift 4 bits (losing the low 4 bits) and compare with the target value similarly rightshifted.

The bitwise and bitshift operators are incredibly useful in embedded applications, for exactly this kind of thing, learn and use them.

0x20 is 32 in decimal, and 0x23 is 35. 0x2F is 47. So you can try

if (32 <= indicatorDirection && indicatorDirection <= 47)

Thanks for the insanely quick response guys!

I now remember about bitshifting which I learned during a programming lesson many years ago. Thanks!

Will test which solution required less memory.

Comparing bitwise with & 0xF0 requires least space.

Comparing in decimal requires 4 more bytes in sketch.

Comparing by rightshifting 4 bits requires and additional 12 more bytes.