^运算符在Java中做了什么? [英] What does the ^ operator do in Java?

查看:186
本文介绍了^运算符在Java中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

^ (插入符号)运算符在Java中用什么函数?

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

当我尝试这样做时:

int a = 5^n;

...它给了我:


对于n = 5,返回0

对于n = 4,返回1

对于n = 6,返回3

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?

推荐答案

Java中的^运算符



^ 是独占或(xor)运算符。

The ^ operator in Java

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

让我们采取 5 ^ 6 例如:

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

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

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

更简单地说,您还可以将xor视为this 那个,但不是两个!。

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

  • Wikipedia: exclusive-or

至于整数取幂,遗憾的是Java确实如此没有这样的运营商。您可以使用 double Math.pow(double,double) (如有必要,将结果转换为 int

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).

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

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.


  • < a href =http://en.wikipedia.org/wiki/Arithmetic_shift =noreferrer>维基百科:算术转换

  • Wikipedia: Arithmetic shift

合并注释 :这个答案是从另一个问题的意图中合并而来的是使用exponentiation将字符串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 ;这个答案的下一部分解决了这个任务不需要取幂的问题。

Merge note: this answer was merged from another question where the intention was to use exponentiation to convert a string "8675309" to int without using Integer.parseInt as 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.



Horner的计划



解决特定的需求,实际上你不需要计算10的各种幂。你可以使用所谓的 Horner的计划,不仅简单而且效率高。

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.

因为你这样做作为个人练习,我不会给出Java代码,但这是主要的想法:

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

一开始可能看起来很复杂,但事实并非如此。你基本上从左到右读取数字,然后在添加下一个数字之前将你的结果乘以10。

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.

以表格形式:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆