^ 运算符在 Java 中做什么?

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

What does the ^ operator do in Java?

javaoperatorsexponentiation

提问by joroj

What function does the ^(caret) operator serve in Java?

^(插入符号)运算符在 Java 中提供什么功能?

When I try this:

当我尝试这个时:

int a = 5^n;

...it gives me:

...它给了我:

for n = 5, returns 0
for n = 4, returns 1
for n = 6, returns 3

对于 n = 5,
对于 n = 4 ,返回 0,对于 n = 6,返回 1
,返回 3

...so I guess it doesn't perform exponentiation. But what is it then?

...所以我想它不会执行幂运算。但那又是什么呢?

回答by AraK

It is the XORbitwise operator.

它是XOR按位运算符。

回答by Dan Dyer

It's bitwise XOR, Java does not have an exponentiation operator, you would have to use Math.pow()instead.

它是按位异或,Java 没有取幂运算符,您必须Math.pow()改用。

回答by Jon Skeet

As others have said, it's bitwise XOR. If you want to raise a number to a given power, use Math.pow(a , b), where ais a number and bis the power.

正如其他人所说,它是按位异或。如果您想将一个数字提高到给定的幂,请使用Math.pow(a , b),其中a是一个数字,b是幂。

回答by Bart Kiers

As already stated by the other answer(s), it's the "exclusive or" (XOR) operator. For more information on bit-operators in Java, see: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

正如其他答案所述,它是“异或”(XOR) 运算符。有关 Java 中位运算符的更多信息,请参见:http: //java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html

回答by Carl Smotricz

AraK's link points to the definition of exclusive-or, which explains how this function works for two boolean values.

AraK 的链接指向异或的定义,它解释了该函数如何处理两个布尔值。

The missing piece of information is how this applies to two integers (or integer-type values). Bitwise exclusive-or is applied to pairs of corresponding binary digits in two numbers, and the results are re-assembled into an integer result.

缺少的信息是这如何应用于两个整数(或整数类型值)。按位异或应用于两个数字中对应的二进制数字对,并将结果重新组合为整数结果。

To use your example:

使用您的示例:

  • The binary representation of 5 is 0101.
  • The binary representation of 4 is 0100.
  • 5 的二进制表示是 0101。
  • 4 的二进制表示是 0100。

A simple way to define bitwise XOR is to say the result has a 1 in every place where the two input numbers differ.

定义按位异或的一种简单方法是说结果在两个输入数字不同的每个地方都有一个 1。

With 4 and 5, the only difference is in the last place; so

对于 4 和 5,唯一的区别是在最后一位;所以

0101 ^ 0100 = 0001 (5 ^ 4 = 1) .

0101 ^ 0100 = 0001 (5 ^ 4 = 1) 。

回答by Mark Byers

As many people have already pointed out, it's the XORoperator. Many people have also already pointed out that if you want exponentiation then you need to use Math.pow.

正如许多人已经指出的那样,它是XOR运算符。很多人也已经指出,如果你想要求幂,那么你需要使用Math.pow

But I think it's also useful to note that ^is just one of a family of operators that are collectively known as bitwise operators:

但我认为注意到这^只是统称为按位运算符的一系列运算符之一也很有用:

Operator    Name         Example     Result  Description
a & b       and          3 & 5       1       1 if both bits are 1.
a | b       or           3 | 5       7       1 if either bit is 1.
a ^ b       xor          3 ^ 5       6       1 if both bits are different.
~a          not          ~3          -4      Inverts the bits.
n << p      left shift   3 << 2      12      Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
n >> p      right shift  5 >> 2      1       Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions.
n >>> p     right shift  -4 >>> 28   15      Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.

From here.

这里开始

These operators can come in handy when you need to read and write to integers where the individual bits should be interpreted as flags, or when a specific range of bits in an integer have a special meaning and you want to extract only those. You can do a lot of every day programming without ever needing to use these operators, but if you ever have to work with data at the bit level, a good knowledge of these operators is invaluable.

当您需要读取和写入整数时,这些运算符会派上用场,其中单个位应被解释为标志,或者当整数中的特定位范围具有特殊含义而您只想提取这些位时。您可以在不需要使用这些运算符的情况下进行大量日常编程,但是如果您必须在位级别处理数据,那么充分了解这些运算符是非常宝贵的。

回答by GuruKulki

It is the bitwise xor operator in java which results 1 for different value (ie 1 ^ 0 = 1) and 0 for same value (ie 0 ^ 0 = 0).

它是java中的按位异或运算符,它对不同的值(即1 ^ 0 = 1)产生1,对相同的值(即0 ^ 0 = 0)产生0。

回答by polygenelubricants

The ^ operator in Java

Java 中的 ^ 运算符

^in Java is the exclusive-or ("xor") operator.

^在 Java 中是异或(“xor”)运算符。

Let's take 5^6as example:

让我们5^6举个例子:

(decimal)    (binary)
     5     =  101
     6     =  110
------------------ xor
     3     =  011

This the truth table for bitwise (JLS 15.22.1) and logical (JLS 15.22.2) xor:

这是按位(JLS 15.22.1)和逻辑(JLS 15.22.2)异或的真值表

^ | 0 1      ^ | F T
--+-----     --+-----
0 | 0 1      F | F T
1 | 1 0      T | T F

More simply, you can also think of xor as "this orthat, but not both!".

更简单地说,您也可以将 xor 视为“这个那个,但不是两者!”。

See also

也可以看看



Exponentiation in Java

Java 中的求幂

As for integer exponentiation, unfortunately Java does not have such an operator. You can use double Math.pow(double, double)(casting the result to intif necessary).

至于整数取幂,不幸的是Java没有这样的运算符。您可以使用double Math.pow(double, double)int必要时将结果转换为)。

You can also use the traditional bit-shifting trick to compute some powers of two. That is, (1L << k)is two to the k-th power for k=0..63.

您还可以使用传统的位移技巧来计算某些 2 的幂。也就是说,(1L << k)是 的k 次k=0..63

See also

也可以看看



Merge note: this answer was merged from another question where the intention was to use exponentiation to convert a string "8675309"to intwithout using Integer.parseIntas a programming exercise (^denotes exponentiation from now on). The OP's intention was to compute 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0 = 8675309; the next part of this answer addresses that exponentiation is not necessary for this task.

合并说明:这个答案是从另一个问题合并而来的,该问题的目的是使用求幂将字符串转换"8675309"int不用Integer.parseInt作编程练习(^从现在开始表示求幂)。OP 的目的是计算8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0 = 8675309; 此答案的下一部分说明此任务不需要求幂。

Horner's scheme

霍纳的计划

Addressing your specificneed, you actually don't need to compute various powers of 10. You can use what is called the Horner's scheme, which is not only simple but also efficient.

针对您的特定需求,您实际上不需要计算 10 的各种幂。您可以使用所谓的Horner 方案,它不仅简单而且高效。

Since you're doing this as a personal exercise, I won't give the Java code, but here's the main idea:

由于您将此作为个人练习,因此我不会提供 Java 代码,但主要思想如下:

8675309 = 8*10^6 + 6*10^5 + 7*10^4 + 5*10^3 + 3*10^2 + 0*10^1 + 9*10^0
        = (((((8*10 + 6)*10 + 7)*10 + 5)*10 + 3)*10 + 0)*10 + 9

It may look complicated at first, but it really isn't. You basically read the digits left to right, and you multiply your result so far by 10 before adding the next digit.

乍一看可能很复杂,但实际上并非如此。您基本上是从左到右读取数字,然后将到目前为止的结果乘以 10,然后再添加下一个数字。

In table form:

表格形式:

step   result  digit  result*10+digit
   1   init=0      8                8
   2        8      6               86
   3       86      7              867
   4      867      5             8675
   5     8675      3            86753
   6    86753      0           867530
   7   867530      9          8675309=final

回答by jcao219

That is because you are using the xor operator.

那是因为您正在使用 xor 运算符。

In java, or just about any other language, ^ is bitwise xor, so of course,

在 Java 或几乎任何其他语言中,^ 是按位异或,所以当然,

10 ^ 1 = 11. more info about bitwise operators

10 ^ 1 = 11. 有关按位运算符的更多信息

It's interesting how Java and C# don't have a power operator.

有趣的是 Java 和 C# 没有幂运算符。