An XOR gate sometimes referred to by its extended name, Exclusive OR gate is a digital logic gate with two or more inputs and one output that performs exclusive disjunction.
The output of an XOR gate is true only when exactly one of its inputs is true. If both of an XOR gate's inputs are false , or if both of its inputs are true , then the output of the XOR gate is false. If an XOR gate has more than two inputs, then its behavior depends on its implementation.
In the vast majority of cases, an XOR gate will output true if an odd number of its inputs is true. However, it's important to note that this behavior differs from the strict definition of exclusive or, which insists that exactly one input must be true for the output to be true. Skip navigation. With the exception of the ternary operator approach, this has a problem that if foo or bar are expressions, and not objects or literals, they will be evaluated twice. This means a slower script, and it may not be desirable to evaluate them twice, if they run functions or methods as part of the evaluation, when the code only expects them to be evaluated once.
For this use, the XOR can be moved into its own function, so that the expressions are only evaluated once, and the function can be reused:. If you only plan to use it once, you can even call the function inline where you define an anonymous function, and immediately run it. Note that this syntax is very ugly, and you should only use it if you want your code to be very hard for anyone else or even yourself to debug:.
It is possible, but the operator expects to be given numbers. Another way in which XOR stands apart from other such operations is to do with linear separability. This is a concept from Artificial Intelligence relating to classification tasks. Suppose we have a set of data that fall into two categories. Our task is to define a single boundary line or, extending the notion to higher dimensions, a hyperplane that neatly partitions the data into its two categories. This is very useful because it gives us the predictive power required to correctly classify new unseen examples.
For example, we might want to identify whether or not someone will default on their mortgage payments using only two clues: their annual income and the size of their property. Figure 6 is a hypothetical example of how this might look. A new mortgage application might be evaluated using this model to determine whether the applicant is likely to default.
Not all problems are neatly separable in this way. That means we either need more than one boundary line, or we need to apply some kind of non-linear transformation into a new space in which it is linearly separable: this is how machine learning techniques such as neural networks and support vector machines work.
The transformation process might be computationally expensive or completely unachievable. For example, the most commonly used and rigorously understood type of neural network is the multi-layer perceptron. With a single layer it is only capable of classifying linearly separable problems.
So where does XOR come into all this? Of all the 16 possible binary Boolean operations, XOR is the only one with its complement, XNOR that is not linearly separable with a single boundary line: two lines are required, as the diagram in Figure 7 demonstrates.
Suppose first of all that you just want to add 2 bits together, so the output is a number between 0 and 2. So with just these two logic gates, we have a module that can add a pair of bits, giving a 2-bit output. This structure is called a half adder and is depicted in Figure 8. For that we need to augment what we have into a full adder. We begin with a half adder to add our two input bits.
Then we need another half adder to add the result to the input carry bit. Finally we use an OR gate to combine the carry bits output by these two half adders into our overall output carry bit. This structure is represented in Figure 9.
Now we are able to chain as many of these adders together as we wish in order to add numbers of any size. The diagram below shows an 8-bit adder array, with the carry bits being passed along from one position to the next. Everything in electronics is modular, so if you want to add bit numbers you could buy four of these components and connect them together see Figure For those comfortable with the mathematics, here is a bit more detail of how XOR fits into group theory.
Asked 10 years, 6 months ago. Active 4 years, 1 month ago. Viewed k times. Dark Star1 Dark Star1 1 1 gold badge 2 2 silver badges 8 8 bronze badges. Add a comment. Active Oldest Votes. Nate Eldredge Nate Eldredge Kennedy I.
0コメント