簡體   English   中英

^ =在Java中是什么意思?

[英]What does ^= mean in Java?

我在HashMap.java中看到了以下代碼。

    h ^= k.hashCode();
    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);

一些隨機輸入產生類似於加法的輸出,但是下面的代碼導致0

   int p = 10;
    p ^= 10;
    System.out.println("_______ " + p);

^=運算符對左側的變量和操作數進行XOR,然后對該結果的變量進行賦值。

與自身進行異或,您得到零。 這是將寄存器設置為零的有效方法,因為它不會移動任何內存。

^被稱為XOR(異或或或) ,是邏輯運算符,它遵循:

1 xor 1 = 0
1 xor 0 = 1
0 xor 1 = 1
0 xor 0 = 0

因為p^=10等效於:

p = p ^ 10; or,  p = p xor 10

p = 10您的操作很簡單:, (10 ^ 10) ,其結果為0

^=是按位異或和賦值運算符。 x ^= 2x = x ^ 2

^是邏輯XOR(異或)的符號。

當放在等號之前時,這意味着“將給定值與自己異或,然后將結果寫回給自己”。 a ^ =值與說a = a ^值相同。

如果與其他運算符(例如add)一起使用,它可能會變得更加清晰。

例如:

int counter = 14;

 // Add 20 to counter (could also be written counter = counter + 20):
counter += 20;

// Counter is now 34

它是Java中的復合賦值運算符

x op= y等效於x = x op y (松散地說,請參見上面的鏈接)。

例如:

x += y; 等價於x = x + y;

x -= y; 等價於x = x - y;

等等 ...


作為參考,這是java中所有此類運算符的完整列表

+= -= *= /= &= |= ^= %= <<= >>= >>>=

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM