简体   繁体   中英

How can I implement XOR Logic in JAVA

folks... Close to my previous question problem. So, in XOR Gate of a logical circuit, if all of input signals are 0 or 1 then the output is 0 ; if the inputs's signals are mix such as 1110001111 then output is 1 ; but if we have signals 0 and X (unknown signal), then the ouput is x; when 1 and X then the output should be X . I have a bug in the code of method because when, let's say 1111100001 signals are fed in the gate, I'm getting 0 instead of 1 . Could smb please help me? How should I handle if the inputs are 0000000 or 11111 ? Note: Signal.HI is 1 , Signal.LO is 0 , Signal.X is X . Please let me know if any more information is needed.

   @Override
    public boolean propagate() 
    {
        Signal inputSignal;
        Signal tempSignal;
        Signal temp = getOutput().getSignal();
        List<Wire> inputs = getInputs();
        Signal result = Signal.LO;

        for(int i = 1; i < inputs.size(); i++)
        {

            inputSignal = inputs.get(i).getSignal();
            tempSignal = inputs.get(0).getSignal();

            if(inputSignal == tempSignal)
                getOutput().setSignal(result);

            /*else if((inputSignal == Signal.LO) && (tempSignal == Signal.LO))
                getOutput().setSignal(result);*/

            else if(inputSignal == Signal.X)
            {
                result = inputSignal;
                getOutput().setSignal(result);
                break;
            }
            else            
                getOutput().setSignal(Signal.HI);
.............................................

Check you 'for-loop'. You are giving an output for every digit you scan. But what I get from your question detail, you want a single output after scanning all the signals.

Signal result = Signal.LO;
tempSignal = inputs.get(0).getSignal();

    for(int i = 1; i < inputs.size(); i++)
    {

        inputSignal = inputs.get(i).getSignal();


        if(inputSignal != tempSignal)
        {
              result = Signal.HI;
              break;
        }

        else if(inputSignal == Signal.X)
        {
            result = inputSignal;
            break;
        }
   }
getOutput().setSignal(result);

You should be systematic in the logic. I would formulate the solution like this:
There is a list of Signal as inputs. You have to check the value of each Signal and build a summary (intermediate result) of what Signal have been processed so far.
In all cases, the process has to reach the end of the inputs, but when there is already one (or more) HI and one (or more) LO. In this case only, we know the output.
The summary can be easily design with just 3 boolean values that hold which kind of Signal have been processed.

I kept the different class methods you provided, but for the Signal I defined an enum which seems a good case and allows to use a switch/case :

enum Signal {
    LO(0), HI(1), X(-1);

    private int value;

    private Signal(int i) {
        this.value = i;
    }
    public int getValue() { return value; }
}

The propagate() method can be something like:

public boolean propagate() {
    // Summary
    boolean lo = false;
    boolean hi = false;
    boolean x = false;

    Iterator<Wire> iw = getInputs().iterator();
    // loop on wires. Stop if there is a LO and a HI
    while (iw.hasNext() && !(lo && hi)) {
        final Wire w = iw.next();
        switch (w.getSignal()) {
        case HI:
            hi = true;
            break;
        case LO:
            lo = true;
            break;
        case X:
            x = true;
            break;
        }
    }
    // Set the output
    if (hi && lo) {
        getOutput().setSignal(Signal.HI);
    } 
    else if (!lo && hi && x || lo && !hi && x) {
        getOutput().setSignal(Signal.X);
    }
    else {
        getOutput().setSignal(Signal.LO);
    }
    // return true if all wires were checked
    return !iw.hasNext();
}

With the above method, results are:

[LO, LO, LO] -> LO
[HI, HI, HI] -> LO
[LO, HI, HI] -> HI
[HI, X, HI] -> X
[LO, LO, X] -> X
[X, X, X] -> LO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM