36. Compare Register Values

#include <iostream>
#include <cstdint>
using namespace std;

class Register8 {
private:
    uint8_t value;
public:
    Register8(uint8_t v) : value(v) {}

    bool operator==(const Register8& other) const {
        return value == other.value;
    }

    uint8_t getValue() const {
        return value;
    }
};

int main() {
    int a, b;
    cin >> a >> b;

    Register8 r1(a);
    Register8 r2(b);

    if (r1 == r2)
        cout << "Equal";
    else
        cout << "Not Equal";

    return 0;
}

Solution Details

  • The operator == compares the private value of two Register8 objects.
     
  • It returns true if equal, false otherwise.
     
  • The if statement in main() then prints the result.

     

👉 In simple words:
 Instead of manually calling a function to compare two registers, you can just use r1 == r2 like built-in types.

Significance for Embedded Developers

  • Useful when comparing hardware register values, configuration states, or sensor outputs.
  • Makes code more readable (if (reg1 == reg2)) instead of writing custom functions.
     
Loading...

Input

10 10

Expected Output

Equal