int equal = 0;
for (int i = 0; i < a.length(); i++) {
equal |= a.charAt(i) ^ b.charAt(i);
}
return equal == 0;
I understand pipe and XOR operator But what is |=
doing?
int equal = 0;
for (int i = 0; i < a.length(); i++) {
equal |= a.charAt(i) ^ b.charAt(i);
}
return equal == 0;
I understand pipe and XOR operator But what is |=
doing?
It is similar to +=. See the table here
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
So it is equivalent to writing your code as:
equal = equal | a.charAt(i) ^ b.charAt(i)
Also as luk2302 has pointed out correctly, that there (bitwise exclusive OR)^
has higher precedence over (bitwise inclusive OR)|
so you can include it inside the brackets like this:
equal = equal | (a.charAt(i) ^ b.charAt(i))
This code is appears to be a great example of why goofballs should not be hired as programmers.
Here is an explanation of the code:
The or-equals operator performs a bitwise or operation between the left hand argument and the right hand argument then assigns the result to the left hand argument. This means that this statement:
left |= right
performs the same work as this statement:
left = (left | right)
It is common for goofballs to regularly reinvent already existing functionality and to do it poorly. In this sense, the code above is a success; it both reinvents existing functionality and does it terribly.
This code exhibits some disturbingly incompetent behaviour
A programmer who is not an idiot would perform a string comparison operation using the String.equals method or, if they are more than just barely competent, they would use a utility like the Apache Commons Lang StringUtils to perform null safe comparisons.
It's simple mate. The following lines do the same thing:
equal |= a.charAt(i) ^ b.charAt(i);
equal = equal | (a.charAt(i) ^ b.charAt(i));