From fb13553bd31ca408dbc301c1aa28e8a2c96e4466 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Sat, 15 Jun 2024 11:49:35 +0200 Subject: Creation of XOR-gate --- GateXor.cpp | 20 ++++++++++++++++++++ GateXor.h | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 GateXor.cpp create mode 100644 GateXor.h 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; +}; -- cgit v1.2.3