[Solved] Golang operator &^ working detail


It’s mentioned in Spec: Arithmetic operators:

&^   bit clear (AND NOT)

So it computes the AND connection of the first operand and the negated value of the second operand.

Its name “bit clear” comes from what it does. Examined using it on two 1-bit value: if the second operand is 1, it means to “clear”: the result will be 0 regardless of what the first operand is. If the second operand is 0, it means not to clear, and the result will be the first operand.

Its Karnaugh-map is:

A | B | A &^ B = A AND NOT(B)
--+---+----------------------
0 | 0 |   0
0 | 1 |   0
1 | 0 |   1
1 | 1 |   0

When used on integer numbers, the operation is carried out bit-wise, that is the 0th bit of the result will be using &^ on the 0th bit of the first number and the second number; the 1st bit of the result will be using &^ on the 1st bit of the fist number and the second number etc…

So basically the result of A &^ B will be the number derived from A by clearing the bits (setting them to 0) marked by the ones in B (in B‘s binary representation).

2

solved Golang operator &^ working detail