aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUnavailableDev <69792062+UnavailableDev@users.noreply.github.com>2024-06-15 11:49:35 +0200
committerUnavailableDev <69792062+UnavailableDev@users.noreply.github.com>2024-06-15 11:49:35 +0200
commitfb13553bd31ca408dbc301c1aa28e8a2c96e4466 (patch)
treeb84f8261675df06c68ecdc8f4b34109ac0c1ecc4
parent96dc1959a05239283b52c62b8f34c3ddcd76700d (diff)
Creation of XOR-gate
-rw-r--r--GateXor.cpp20
-rw-r--r--GateXor.h22
2 files changed, 42 insertions, 0 deletions
diff --git a/GateXor.cpp b/GateXor.cpp
new file mode 100644
index 0000000..84fb1ec
--- /dev/null
+++ b/GateXor.cpp
@@ -0,0 +1,20 @@
+#include "GateXor.h"
+
+GateXor GateXor::instance(GateXor::type);
+
+SignalLevel GateXor::level() {
+ int highCount = 0;
+ for (int i = 0; i < this->inputs.size(); i++) {
+ SignalLevel l = this->inputs[i]->getLevel();
+
+ if (l == UNDEFINED) return UNDEFINED;
+ if (l == HIGH) highCount++;
+ }
+ return (highCount % 2 == 1) ? HIGH : LOW;
+}
+
+GateXor::GateXor(const GateXor *prototype) : Node() {}
+
+GateXor *GateXor::clone() const {
+ return new GateXor(this);
+}
diff --git a/GateXor.h b/GateXor.h
new file mode 100644
index 0000000..37903e0
--- /dev/null
+++ b/GateXor.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "Node.h"
+
+class GateXor : public Node {
+public:
+ GateXor() = default;
+ GateXor(const GateXor * prototype);
+ ~GateXor() = default;
+ virtual GateXor * clone() const;
+
+private:
+ SignalLevel level();
+
+ using Node::Node;
+ constexpr static const char * type = "xor";
+ static GateXor instance;
+
+private:
+ int min_inputs = 1;
+ int max_inputs = -1;
+};