8-Bit Register Class

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

// Define Reg8 class here
class Reg8{

    uint8_t value;
    public:
    Reg8(){
        value = 0x00;
    }

    void write(uint8_t v){
        value = v; 
    }

    void setBit(int bs){
        value |= (0x01<<bs);
    }

    void clearBit(int bc){
        value &= ~(0x01<<bc);
    }

    uint8_t read(){
        return value;
    }
};

int main() {
    int initialValue;
    int bitToSet, bitToClear;

    cin >> initialValue >> bitToSet >> bitToClear;

    Reg8 reg;
    reg.write(static_cast<uint8_t>(initialValue));
    reg.setBit(bitToSet);
    reg.clearBit(bitToClear);

    cout << static_cast<int>(reg.read());
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

10 1 3

Expected Output

2