Byte digits

Hello,

I have to change a byte based on the first three digits of another byte.

So if i have

byte a1= B01001100;
byte b2= B00000000;

and i want b2 to be B10000000 because the first three digits of a1 are 010.

What would be the best way to do that?

I don't quite see the relation between the 010 and the resulting 100. If this is a typo and you want to make the upper three bits of b2 equal to the upper three bits of a1 then:

// clear upper three bits of b2
b2 = b2 & 0x1F;
// clear lower 5 bits of a1
a1 = a1 & 0xE0;
// make the upper three bits equal
b2 = b2 | a1;

Edit: If you want to compare the upper three bits of a1 and 0b01000000 then:

// clear lower 5 bits of a1
byte temp = a1 & 0xE0;
// check if equal
if(temp == 0b01000000)
{
    // set b2 if equal
    b2 = 0b10000000;
}