^ 运算符在 Java 中有什么作用? [英] What does the ^ operator do in Java?
问题描述
^
(插入符号)运算符在 Java 中的作用是什么?
当我尝试这个时:
int a = 5^n;
...它给了我:
<块引用>对于 n = 5,返回 0
对于 n = 4,返回 1
对于 n = 6,返回 3
...所以我猜它不会执行幂运算.但那又是什么呢?
Java 中的 ^ 运算符
^
在 Java 中是异或(xor")运算符.
我们以5^6
为例:
(十进制)(二进制)5 = 1016 = 110------------------ 异或3 = 011
这是按位的真值表 (JLS 15.22.1)和逻辑(JLS 15.22.2) 异或:
<代码>^ |0 1 ^ |傅立叶--+----- --+-----0 |0 1 楼 |傅立叶1 |1 0 吨 |TF
更简单地说,您也可以将异或视为这个或那个,但不能同时!".
另见
<小时>Java 中的求幂
至于整数取幂,不幸的是Java没有这样的运算符.您可以使用 double Math.pow(double, double)
(必要时将结果转换为 int
).
您还可以使用传统的位移技巧来计算 2 的某些幂.也就是说,(1L <
>k=0..63
的 k 次方.
另见
<小时><块引用>合并注释:这个答案是从另一个问题合并而来的,该问题的目的是使用幂运算将字符串 "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
;此答案的下一部分说明此任务不需要求幂.
霍纳的计划
解决您的特定需求,您实际上不需要计算 10 的各种幂.您可以使用所谓的 Horner 的方案,不仅简单而且高效.
因为你是作为个人练习来做这个的,我不会给出 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
乍一看可能很复杂,但实际上并非如此.您基本上是从左到右读取数字,然后将到目前为止的结果乘以 10,然后再添加下一个数字.
表格形式:
step result digit 结果*10+digit1 初始化=0 8 82 8 6 863 86 7 8674 867 5 86755 8675 3 867536 86753 0 8675307 867530 9 8675309=最终
What function does the ^
(caret) operator serve in 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
...so I guess it doesn't perform exponentiation. But what is it then?
The ^ operator in Java
^
in Java is the exclusive-or ("xor") operator.
Let's take 5^6
as example:
(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:
^ | 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 or that, but not both!".
See also
Exponentiation in 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 int
if necessary).
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
.
See also
Merge note: this answer was merged from another question where the intention was to use exponentiation to convert a string
"8675309"
toint
without usingInteger.parseInt
as a programming exercise (^
denotes exponentiation from now on). The OP's intention was to compute8*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.
Horner's scheme
Addressing your specific need, 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.
Since you're doing this as a personal exercise, I won't give the Java code, but here's the main idea:
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.
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
这篇关于^ 运算符在 Java 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!