Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

xor/xnor gate implementation in java

800577Jan 14 2009 — edited May 3 2009
here is the code that implements the xor gate for my program...
    public static boolean xor(boolean... input)             //for n inputs
    {
        boolean result=input[0];                            //assigns 1st value to result
        for(int i=0; i<input.length-1;)                     //loops till length-1
        {
            result=(result&&!input[i+1])||(!result&&input[i+1]);                      //does A*B'+A'*B
            i++;
        }
        return result;
    }
the code gives the right output for 3 inputs...

now here is the code that implements the xnor gate.
    public static boolean xnor(boolean... input)             //for n inputs
    {
        boolean result=input[0];                            //assigns 1st value to result
        for(int i=0; i<input.length-1;)                     //loops till length-1
        {
            result=(result&&input[i+1])||((!result)&&(!input[i+1]));                      //does A*B+A'*B'
            i++;
        }
        return result;
 }
the output of this for a given set of input should be exactly the opposite of the output of xor gate. however, the output comes out to be the same!

i have checked my code several times and its all right. infact if i replace the above code with the following the output turns out to be fine...
    public static boolean xnor(boolean... input)             //for n inputs
    {
        return(!XOR.xor(input));
    }
any help?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 31 2009
Added on Jan 14 2009
20 comments
1,119 views