Java 两个短整数的异或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9856079/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:22:21  来源:igfitidea点击:

XOR of two short integers

javaxor

提问by Ravi Joshi

I am calculating XORof two short integersusing XOR ^operator in a traditional fashion. Below is the method-

我正在以传统方式使用运算符计算XOR两个。下面是方法——short integersXOR ^

short a=197;
short b=341;
short y = (short) (a ^ b);

However the XOR always returned integer but in my case inputs are short integer, that is why i am casting short to the XOR output. The XOR can be calculated in different manners (example: using BigIntegeretc.) but performance wise (less time) which is the best for short integers? While keeping performance in mind, should i first convert each short integer to binary number using Integer.toBinaryString(number)then apply bitwise XOR?

然而,异或总是返回整数,但在我的情况下,输入是短整数,这就是为什么我将短转换为异或输出。XOR 可以以不同的方式计算(例如:使用BigInteger等),但性能方面(更少的时间)哪个最适合短整数?在牢记性能的同时,我是否应该首先使用Integer.toBinaryString(number)然后应用按位异或将每个短整数转换为二进制数?

采纳答案by Jeffrey

short s1 = ...
short s2 = ...
short result = (short) (s1 ^ s2);

This is the most efficient way to XOR two shorts together. It does not run into the overhead of creating BigIntegers and the cast will never cause an overflow issue as both s1and s2are shorts to begin with.

这是对两个shorts进行异或的最有效方法。它不会遇到创建BigIntegers的开销,并且强制转换永远不会导致溢出问题,因为ss1s2are 都以shorts 开始。

回答by Jon Skeet

It's not really clear what you mean by "convert each short integer to binary number" - a short is already a number, and its representation is naturally binary anyway.

“将每个短整数转换为二进制数”是什么意思并不是很清楚——short 已经是一个数字,无论如何它的表示自然是二进制。

You just want:

你只想要:

short x = ...;
short y = ...;
short z = (short) (x ^ y);

You need the cast as x ^ ywill promote both to int, and the result will be an int. However, the result will have to be in the range of a shortanyway, so it's safe to perform this cast without losing information.

您需要x ^ y将两者都提升到的演员表int,结果将是int. 但是,short无论如何,结果都必须在 a 的范围内,因此执行此转换而不会丢失信息是安全的。

See section 15.22.1 of the JLSfor more information about XOR in particular and section 5.6.2for information on binary numeric promotion in general.

请参阅JLS 的第 15.22.1 节以了解有关 XOR 的更多信息,以及有关二进制数字提升的一般信息的第 5.6.2 节

回答by Eric J.

I'm not 100% sure what you're asking, but hopefully this helps:

我不是 100% 确定你在问什么,但希望这会有所帮助:

Java coerces both operands to type int. That is why the result is an int.

Java 将两个操作数强制为 int 类型。这就是结果是 int 的原因。

so your shorts will be automatically converted to an int, and the XOR operation will be done very efficiently on the integer operands.

所以你的shorts将自动转换为int,并且XOR操作将在整数操作数上非常有效地完成。

If one of the operands is a long, both types are instead coerced to a long. However, that does not apply in your case.

如果其中一个操作数是 long,则两种类型都被强制转换为 long。但是,这不适用于您的情况。

Bottom line, given that both of your inputs are short, if you need a short result, the most efficient thing to do is

最重要的是,鉴于您的两个输入都很短,如果您需要一个简短的结果,最有效的做法是

short result = (short) (operandA ^ operandB);