What does the ^ operator do in Java?

2018-12-31 08:21发布

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?

17条回答
君临天下
2楼-- · 2018-12-31 08:46

In other languages like Python you can do 10**2=100, try it.

查看更多
妖精总统
3楼-- · 2018-12-31 08:48

XOR operator rule

0 ^ 0 = 0
1 ^ 1 = 0
0 ^ 1 = 1
1 ^ 0 = 1

Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101



a^b ==> 0011 1100  (a)
        0000 1101  (b)
        -------------  XOR
        0011 0001  => 49

(a ^ b) will give 49 which is 0011 0001
查看更多
旧时光的记忆
4楼-- · 2018-12-31 08:48

It is the XOR bitwise operator.

查看更多
大哥的爱人
5楼-- · 2018-12-31 08:49

As many people have already pointed out, it's the XOR operator. Many people have also already pointed out that if you want exponentiation then you need to use 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.

查看更多
不再属于我。
6楼-- · 2018-12-31 08:49

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

查看更多
深知你不懂我心
7楼-- · 2018-12-31 08:49

Lot many people have already explained about what it is and how it can be used but apart from the obvious you can use this operator to do a lot of programming tricks like

  • XORing of all the elements in a boolean array would tell you if the array has odd number of true elements
  • If you have an array with all numbers repeating even number of times except one which repeats odd number of times you can find that by XORing all elements.
  • Swapping values without using temporary variable
  • Finding missing number in the range 1 to n
  • Basic validation of data sent over the network.

Lot many such tricks can be done using bit wise operators, interesting topic to explore.

查看更多
登录 后发表回答