Creating a "Logical Exclusive Or" Operator in Java

Creating a logical exclusive or operator in Java

Java does have a logical XOR operator, it is ^ (as in a ^ b).

Apart from that, you can't define new operators in Java.

Edit: Here's an example:

public static void main(String[] args) {
boolean[] all = { false, true };
for (boolean a : all) {
for (boolean b: all) {
boolean c = a ^ b;
System.out.println(a + " ^ " + b + " = " + c);
}
}
}

Output:


false ^ false = false
false ^ true = true
true ^ false = true
true ^ true = false

What is the logical xor operator in java?

The logical XOR operator does exist in Java and is spelled ^.

To get the terminology right, in Java:

  • &, | and ^ are called bitwise or logical operators, depending on the types of their arguments;
  • && and || are called conditional operators.

For details, see JLS § 15.22. Bitwise and Logical Operators onwards.

There is no direct equivalent for && and || for XOR. The only reason && and || exist as separate operators from & and | is their short-circuiting behaviour (that's why they're called "conditional"), and XOR cannot be short-circuited.

java operator and exclusive with boolean operands

You're using xor : ^.

true ^ false == true

See also:

  • Is it good practice to use the XOR (^) operator in Java for boolean checks?
  • Creating a "logical exclusive or" operator in Java

I'm actually not sure what you mean by "exclusive and". See also:

  • What are XAND and XOR

^ operator in java

This is the same as ^ in most languages, just an XOR.

false ^ false == false
true ^ false == true
false ^ true == true
true ^ true == false

What is the logical xor operator in java?

The logical XOR operator does exist in Java and is spelled ^.

To get the terminology right, in Java:

  • &, | and ^ are called bitwise or logical operators, depending on the types of their arguments;
  • && and || are called conditional operators.

For details, see JLS § 15.22. Bitwise and Logical Operators onwards.

There is no direct equivalent for && and || for XOR. The only reason && and || exist as separate operators from & and | is their short-circuiting behaviour (that's why they're called "conditional"), and XOR cannot be short-circuited.

Logical expressions in java

^ is the XOR operator in Java.


Regarding your Swing problem, the problem is that you are not inspecting the state of the checkboxes when the button is clicked, but rather when the checkboxes are selected. You should instead have something like this:

private class ButtonActionListener implements ActionListener {

/*
* You will probably define a constructor that accepts the two checkboxes
* as arguments.
*/

@Override
public void actionPerformed(ActionEvent event) {
if (one.isSelected() ^ two.isSelected()) {
dispose();
}
}
}

The alternative approach is to create one instance of the ActionListener. You would add it with addActionListener when exactly one of the checkboxes is checked, and remove it with removeActionListener otherwise:

private class CheckBoxListener implements ItemListener {

private ActionListener buttonActionListener = new thehandler();

@Override
public void itemStateChanged(ItemEvent event) {
if(one.isSelected() ^ two.isSelected()) {
button.addActionListener(buttonActionListener);
} else {
button.removeActionListener(buttonActionListener);
}
}
}

Is it good practice to use the xor operator for boolean checks?

You can simply use != instead.



Related Topics



Leave a reply



Submit