Register Operator Overloading

#include <cstdint>
#include <cstdio>

// Register abstraction (USER MUST MODIFY THIS CLASS ONLY)
class Register32 {
private:
    std::uint32_t value;
public:
    Register32(uint32_t x) : value(x) {};
    Register32& operator|=(uint32_t x)
    {
        value |= x;
        return *this;
    }

    Register32& operator&=(uint32_t x)
    {
        value &= x;
        return *this;
    }

    uint32_t get() const
    {
        return value;
    }
};

int main() {
    Register32 reg{0};

    int n;
    std::scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        char op[6];
        int bit;
        std::scanf("%s %d", op, &bit);

        // MUST REMAIN UNCHANGED
        if (op[0] == 'S') {
            reg |= (1u << bit);
        } else {
            reg &= ~(1u << bit);
        }
    }

    std::printf("reg=%u\n", reg.get());
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

3 SET 0 SET 3 CLEAR 0

Expected Output

reg=8