/**
* @return true if exactly one of the given parameters is true.
*/
boolean xOrTest(boolean a, boolean b, boolean c, boolean d) {
if (a ^ b ^ c ^ d) {
return true;
}
return false;
}
but this does not work in the following scenario (remember I want the method to return false because more than one parameter is true).
System.out.println(xOrTest(false, true, true, true));
infact returns true. My question is, can someone explain why ^ behaves this way and can ^ be used to efficiently meet the requirement?System.out.println(xOrTest(false, false, true, true));
System.out.println(xOrTest(false, false, false, true));
System.out.println(xOrTest(false, false, false, false));
System.out.println(xOrTest(true, true, true, true));
The following scenarios all work as I would expect and print true:
System.out.println(xOrTest(true, false, false, false));
System.out.println(xOrTest(false, true, false, false));
System.out.println(xOrTest(false, false, true, false));
System.out.println(xOrTest(false, false, false, true));
regards... infact returns true. My question is,It's supposed to work this way. The calculation goes like this:
can someone explain why ^ behaves this way and can ^
be used to efficiently meet the requirement?
int count = 0;
if (a) count++;
if (b) count++;
if (c) count++;
if (d) count++;
return count == 1;
boolean xOrTest(boolean a, boolean b, boolean c, boolean d) {
int trueCount = 0;
if (a) {
trueCount++;
}
if (b) {
trueCount++;
}
if (c) {
trueCount++;
}
if (d) {
trueCount++;
}
return trueCount==1;
}
System.out.println(xOrTest(false, false, true, true));
System.out.println(xOrTest(false, false, false, false));
System.out.println(xOrTest(true, true, true, true));
About time to rename the method ... :-)...boolean xOrTest(boolean a, boolean b, boolean c, boolean d) {
>
About time to rename the method ... :-)Yes indeed! Thank you for your very speedy resposes
I wouldn't care about efficiency in this kind of methods. Only optimize,I agree with you, and when you recommend to basically "write dumb code", this interview with Brian Goetz crosses my mind: http://java.sun.com/developer/technicalArticles/Interviews/goetz_qa.html
when you find out that this method is indeed a bottleneck, otherwise just
write a simple implementation (simple implementations are usually easier
to optimize for the JIT compiler, than "clever" impelementations).
boolean xor(final boolean a, final boolean b, final boolean c, final boolean d) {
return !((a & (b | c | d)) || (b & (c | d)) || (c & d));
}
I consider this an acceptable solution, too.// (a & !(b | c | d)) | (b & !(a | c | d)) | (c & !(a | b | d)) | (d & !(a | b | c))
// = (a & !b & !c & !d) | (b & !a & !c & !d) | (c & !a & !b & !d) | (d & !a & !b & !c)
// = (((a & !b) | (!a & b)) & !c & !d) | (!a & !b & ((c & !d) | (!c & d)))
// = ((a ^ b) & !c & !d) | (!a & !b & (c ^ d))
which can be coded like:
boolean xor(final boolean a, final boolean b, final boolean c, final boolean d) {
return ((a ^ b) && !c && !d) || (!a && !b && (c ^ d));
}
Umm, well... so much for the "simple straightforward approach"...