Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xor Bytes in Java

Tags:

java

casting

xor

Java has no support for bit-wise operations on bytes. I would like to xor two bytes like so:

xoredByte = byte1 ^ byte2;

In java this would have to be done as:

xoredByte = (byte) (byte1 ^ byte2);

Which compiles to:

xoredByte = (byte) ((int)byte1 ^ (int)byte2);

Does this work in all cases? What I mean is, are these equivalent statements?

If not, what would the code be to perform this operation?

like image 203
Kent Avatar asked Jun 02 '14 22:06

Kent


1 Answers

Yes, both statements are equivalent. When working with binary operators in general, including ^, Java applies "binary numeric promotion" to both operands to ensure that they are both at least int values. Section 5.6.2 of the JLS covers this:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

  • If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  • Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

  • If either operand is of type double, the other is converted to double.

  • Otherwise, if either operand is of type float, the other is converted to float.

  • Otherwise, if either operand is of type long, the other is converted to long.

  • Otherwise, both operands are converted to type int.

(emphasis mine)

and

Binary numeric promotion is performed on the operands of certain operators:

  • The multiplicative operators *, /, and % (§15.17)

  • The addition and subtraction operators for numeric types + and - (§15.18.2)

  • The numerical comparison operators <, <=, >, and >= (§15.20.1)

  • The numerical equality operators == and != (§15.21.1)

  • The integer bitwise operators &, ^, and | (§15.22.1)

  • In certain cases, the conditional operator ? : (§15.25)

(emphasis mine)

Whether you apply the (int) casts or not, byte1 and byte2 will both be promoted to int before the operation. That is also why the cast back to (byte) is necessary.

like image 67
rgettman Avatar answered Sep 20 '22 23:09

rgettman