user1158641
user1158641

Reputation: 47

Match byte patterns

Say you are having a byte of pattern:

byte b = 0x%1;

How to tell when a byte does have certain values on the "2nd position" - in place of % ? In this example 1, no matter what the 1st position holds.

Upvotes: 1

Views: 259

Answers (2)

dacwe
dacwe

Reputation: 43504

Use a mask bits to get the last 8 bits:

int last8bits = b & 0xF;

Edit: You should read up on bitwise operations.


Full example:

public static void main(String[] args) {

    byte b = (byte) 0xA1;
    int last8bits = b & 0xF;

    if (last8bits == 0x01)
        System.out.println("\"matches\"");
}

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691685

if ((0x0F & b) == 0x01) {
    // pattern matched

Upvotes: 1

Related Questions