#include <iostream>
#include <string>
using namespace std;
class Register8 {
private:
unsigned char value;
public:
Register8() : value(0) {}
void setBit(int pos) {
value |= static_cast<unsigned char>(1u << pos);
}
void clearBit(int pos) {
value &= static_cast<unsigned char>(~(1u << pos));
}
void toggleBit(int pos) {
value ^= static_cast<unsigned char>(1u << pos);
}
unsigned char getValue() const {
return value;
}
};
int main() {
Register8 reg;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
string op;
int pos;
cin >> op >> pos;
if (op == "set") {
reg.setBit(pos);
} else if (op == "clear") {
reg.clearBit(pos);
} else if (op == "toggle") {
reg.toggleBit(pos);
}
}
cout << (int)reg.getValue();
return 0;
}
Solution Details
Significance for Embedded Developers: This models a GPIO/control register abstraction. By confining updates to methods, you reduce the risk of corrupting reserved bits and create a reusable, testable unit that mirrors how HALs manage hardware registers.
Input
3 set 0 set 2 toggle 2
Expected Output
1