Reputation:
I found some code like this when overrides the GetHashCode
public override int GetHashCode
{
return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode()
}
what's the symbol of "^"? is it power math function?
Upvotes: 0
Views: 224
Reputation: 45252
It's a bitwise XOR operator.
0 ^ 0 = 0
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
This is a useful way to combine two hash values to create a new hash value.
Applying this to another example: 6 ^ 10 = 12
:
| Binary | Decimal |
|--------|---------|
| 0110 | 6 |
| 1010 | 10 |
|========|=========|
| 1100 | 12 |
|========|=========|
Upvotes: 21