Java中的异或字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24004579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xor Bytes in Java
提问by Kent
Java has no support for bit-wise operations on bytes. I would like to xor two bytes like so:
Java 不支持对字节进行按位操作。我想像这样对两个字节进行异或:
xoredByte = byte1 ^ byte2;
In java this would have to be done as:
在 java 中,这必须是这样完成的:
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?
如果没有,执行此操作的代码是什么?
采纳答案by rgettman
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 JLScovers this:
是的,这两种说法是等价的。通常使用二元运算符时,包括^
,Java 对两个操作数应用“二进制数字提升”,以确保它们都至少是int
值。 JLS 的第 5.6.2 节涵盖了这一点:
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.
当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示一个可转换为数字类型的值,以下规则依次适用:
如果任何操作数是引用类型,则需要进行拆箱转换(第 5.1.8 节)。
扩展原语转换(第 5.1.2 节)用于转换一个或两个操作数,如以下规则所指定:
如果任一操作数的类型为 double,则另一个将转换为 double。
否则,如果任一操作数的类型为 float,则另一个将转换为 float。
否则,如果任一操作数的类型为 long,则另一个将转换为 long。
否则,两个操作数都被转换为 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)
对某些运算符的操作数执行二进制数字提升:
乘法运算符 *、/ 和 %(第 15.17 节)
数字类型 + 和 - 的加法和减法运算符(第 15.18.2 节)
数值比较运算符 <、<=、> 和 >=(第 15.20.1 节)
数值相等运算符 == 和 !=(第 15.21.1 节)
整数按位运算符 &、^ 和 | (第 15.22.1 节)
在某些情况下,条件运算符 ? :(第 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.
无论你申请的(int)
类型转换与否,byte1
并byte2
都将被提升到int
操作之前。这也是为什么强制转换(byte)
为必要的原因。
回答by ajb
What happens is that when the value is cast from byte
to int
, 24 new bits are inserted in front of the 8 bits of a byte. These bits could be all 0's or all 1's (since byte
is a signed type). The result of ^
could therefore have 24 zeroes or 24 ones as the most significant bits. However, when the result is cast back to byte
, those 24 bits are thrown away, no matter what they are; and since ^
is done on a bit-by-bit basis, the low-order 8 bits are the same as they would be if there were an ^
operation on bytes--the upper 24 bits don't have any effect on the lower 8.
发生的情况是,当值从byte
to 转换时int
,会在字节的 8 位之前插入 24 位新位。这些位可以全为 0 或全为 1(因为byte
是有符号类型)。因此,的结果^
可能有 24 个零或 24 个 1 作为最高有效位。但是,当结果被强制转换为 时byte
,这 24 位将被丢弃,无论它们是什么;并且由于^
是逐位完成的,因此低 8 位与^
对字节进行操作时的情况相同——高 24 位对低 8 位没有任何影响。