Java create a byte by XOR 2 bytes

You need to convert them to integers (no loss, primitive widening), do the XOR, then convert the resulting int back to a byte using a bit mask.

// convert to ints and xor
int one = (int)byte1[0];
int two = (int)byte2[0];
int xor = one ^ two;

// convert back to byte
byte b = (byte)(0xff & xor);

Example

String a        = "10101010";
String b        = "01010101";
String expected = "11111111";  // expected result of a ^ b

int aInt = Integer.parseInt(a, 2);
int bInt = Integer.parseInt(b, 2);
int xorInt = Integer.parseInt(expected, 2);

byte aByte = (byte)aInt;
byte bByte = (byte)bInt;
byte xorByte = (byte)xorInt;

// conversion routine compacted into single line
byte xor = (byte)(0xff & ((int)aByte) ^ ((int)bByte));


System.out.println(xorInt + "   // 11111111  as integer");
System.out.println(xorByte + "    // 11111111  as byte");

System.out.println(aInt + "   // a as integer");
System.out.println(bInt + "    // b as integer");
System.out.println((aInt ^ bInt) + "   // a ^ b as integers");

System.out.println(aByte + "   // a as byte");
System.out.println(bByte + "    // b as byte");

System.out.println(xor + "    // a ^ b as bytes");

Prints the following output

255   // 11111111  as integer
-1    // 11111111  as byte

170   // a as integer
85    // b as integer
255   // a ^ b as integers

-86   // a as byte
85    // b as byte
-1    // a ^ b as bytes

You can use the xor operation on bytes. It's the caret (^).

Example:

byte3[0] = (byte) (byte1[0] ^ byte2[0]);