djd
djd

Reputation: 173

Solidity: What does the caret ^ operator do?

I searched solidity documentation and there's nothing on this:

What does the caret ^ operator do?

What is it doing here?

sha3(sha3(valueA) ^ sha3(valueB))

Upvotes: 2

Views: 575

Answers (4)

Yilmaz
Yilmaz

Reputation: 49681

It is xor or exclusive or operator. output is true if the inputs are not alike otherwise the output is false.

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

from here

The XOR Encryption algorithm is a very effective yet easy to implement method of symmetric encryption. Due to its effectiveness and simplicity, the XOR Encryption is an extremely common component used in more complex encryption algorithms used nowadays.

The XOR encryption algorithm is an example of symmetric encryption where the same key is used to both encrypt and decrypt a message.

Also Why is XOR used in cryptography?

Upvotes: 0

user11844224
user11844224

Reputation:

Per https://www.tutorialspoint.com/solidity/solidity_operators.htm (via Google, BTW), as in many languages, it means XOR (exclusive or):

x y  x^y
0 0  0
0 1  1
1 0  1
1 1  0
(1=True; 0=False)

Upvotes: 1

user11844224
user11844224

Reputation:

The keyword I left out is "bitwise" (as Nathan mentioned).

So, if, for example,               sha3(valueA)=0x44=0b01000100, and
                                   sha3(valueB)=0x34=0b00110100, then:
(sha3(valueA)=0x44=0b01000100 and sha3(valueB))=0x70=0b01110000

PS fun fact: XOR with all 1s is a "bit flip"

Upvotes: 0

Nathan
Nathan

Reputation: 5259

It's a bitwise operation, XOR (exclusive or): https://docs.soliditylang.org/en/v0.8.6/types.html

"A bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only one of the bits is 1, but will be 0 if both are 0 or both are 1." https://en.wikipedia.org/wiki/Bitwise_operation#:~:text=A%20bitwise%20XOR%20is%20a,0%20or%20both%20are%201.

Upvotes: 2

Related Questions