Asked • 04/04/19

In Java XOR with three true inputs returns true. Why?

The following code System.out.println("1 0 0: " + (true ^ false ^ false)); System.out.println("1 0 1: " + (true ^ false ^ true)); System.out.println("1 1 0: " + (true ^ true ^ false)); System.out.println("1 1 1: " + (true ^ true ^ true)); System.out.println("0 0 0: " + (false ^ false ^ false)); System.out.println("0 0 1: " + (false ^ false ^ true)); System.out.println("0 1 0: " + (false ^ true ^ false)); System.out.println("0 1 1: " + (false ^ true ^ true)); outputs: 1 0 0: true 1 0 1: false 1 1 0: false 1 1 1: true 0 0 0: false 0 0 1: true 0 1 0: true 0 1 1: false Why does XOR returns `true` when all three inputs are `true`? If it's valid logic how can I implement logic that returns `true` **only** if one of the input elements is `true` (no matter how many inputs I provide)?

1 Expert Answer

By:

Matthew F. answered • 04/05/19

Tutor
New to Wyzant

Professional Software/ Web Developer Specializing in Java

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.