What does the ^ operator do in Java?

前端 未结 17 1762
执念已碎
执念已碎 2020-11-22 03:27

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

When I try this:

int a = 5^n;

...it gives me:

相关标签:
17条回答
  • 2020-11-22 03:46

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

    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.

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

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

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

    0 讨论(0)
  • 2020-11-22 03:46

    It is XOR operator. It is use to do bit operations on numbers. It has the behavior such that when you do a xor operation on same bits say 0 XOR 0 / 1 XOR 1 the result is 0. But if any of the bits is different then result is 1. So when you did 5^3 then you can look at these numbers 5, 6 in their binary forms and thus the expression becomes (101) XOR (110) which gives the result (011) whose decimal representation is 3.

    0 讨论(0)
  • 2020-11-22 03:48

    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

    • Wikipedia: exclusive-or

    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

    • Wikipedia: Arithmetic shift

    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'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
    
    0 讨论(0)
  • 2020-11-22 03: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
    
    0 讨论(0)
  • 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 a is a number and b is the power.

    0 讨论(0)
  • 2020-11-22 03:50

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

    0 讨论(0)
提交回复
热议问题