|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +/** |
| 6 | + * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR) |
| 7 | + */ |
| 8 | +public final class BooleanAlgebraGates { |
| 9 | + |
| 10 | + private BooleanAlgebraGates() { |
| 11 | + // Prevent instantiation |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Represents a Boolean gate that takes multiple inputs and returns a result. |
| 16 | + */ |
| 17 | + interface BooleanGate { |
| 18 | + /** |
| 19 | + * Evaluates the gate with the given inputs. |
| 20 | + * |
| 21 | + * @param inputs The input values for the gate. |
| 22 | + * @return The result of the evaluation. |
| 23 | + */ |
| 24 | + boolean evaluate(List<Boolean> inputs); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * AND Gate implementation. |
| 29 | + * Returns true if all inputs are true; otherwise, false. |
| 30 | + */ |
| 31 | + static class ANDGate implements BooleanGate { |
| 32 | + @Override |
| 33 | + public boolean evaluate(List<Boolean> inputs) { |
| 34 | + for (boolean input : inputs) { |
| 35 | + if (!input) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + return true; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * OR Gate implementation. |
| 45 | + * Returns true if at least one input is true; otherwise, false. |
| 46 | + */ |
| 47 | + static class ORGate implements BooleanGate { |
| 48 | + @Override |
| 49 | + public boolean evaluate(List<Boolean> inputs) { |
| 50 | + for (boolean input : inputs) { |
| 51 | + if (input) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * NOT Gate implementation (Unary operation). |
| 61 | + * Negates a single input value. |
| 62 | + */ |
| 63 | + static class NOTGate { |
| 64 | + /** |
| 65 | + * Evaluates the negation of the input. |
| 66 | + * |
| 67 | + * @param input The input value to be negated. |
| 68 | + * @return The negated value. |
| 69 | + */ |
| 70 | + public boolean evaluate(boolean input) { |
| 71 | + return !input; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * XOR Gate implementation. |
| 77 | + * Returns true if an odd number of inputs are true; otherwise, false. |
| 78 | + */ |
| 79 | + static class XORGate implements BooleanGate { |
| 80 | + @Override |
| 81 | + public boolean evaluate(List<Boolean> inputs) { |
| 82 | + boolean result = false; |
| 83 | + for (boolean input : inputs) { |
| 84 | + result ^= input; |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * NAND Gate implementation. |
| 92 | + * Returns true if at least one input is false; otherwise, false. |
| 93 | + */ |
| 94 | + static class NANDGate implements BooleanGate { |
| 95 | + @Override |
| 96 | + public boolean evaluate(List<Boolean> inputs) { |
| 97 | + return !new ANDGate().evaluate(inputs); // Equivalent to negation of AND |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * NOR Gate implementation. |
| 103 | + * Returns true if all inputs are false; otherwise, false. |
| 104 | + */ |
| 105 | + static class NORGate implements BooleanGate { |
| 106 | + @Override |
| 107 | + public boolean evaluate(List<Boolean> inputs) { |
| 108 | + return !new ORGate().evaluate(inputs); // Equivalent to negation of OR |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments