apache-commons-lang

The result of xor(true, true, true) of the BooleanUtils class under the commons-lang toolkit under Apache is false, why?


The result of xor(true, true, true) of the BooleanUtils class under the commons-lang toolkit under Apache is false, but the result of System.out.println(true ^ true ^ true) is true. Why?

public class Test {
    public static void main(String[] args) {
        System.out.println(org.apache.commons.lang.BooleanUtils.xor(new boolean[]{true, true, true}));
        System.out.println(org.apache.commons.lang3.BooleanUtils.xor(new boolean[]{true, true, true}));
        System.out.println(true ^ true ^ true);
    }
}
/*
    result:
    false
    false
    true
*/

Solution

  • The most likely reason you are seeing this behavior is that you are using an older version of commons-lang (< 3.2).

    Newer versions behave the same as Java (i.e. it evaluates one xor at a time from left to right).

    The older versions used a different approach however: They return true only if there is exactly one true value in the entire array.

    This behavior was considered incorrect (see LANG-921) and has since been fixed.