8-Bit Register Class

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

// Define Reg8 class here

class Reg8 {
    private:
    uint8_t registerValue;

    public:

      Reg8(){
        registerValue = 0;
      }
      void write (uint8_t v){
        registerValue = v;
      }
      uint8_t read(){
        return registerValue;
      }
      void setBit(int bit){
           if (bit >= 0 && bit <= 7) {
            registerValue |= (1 << bit);
           }
      }
      void clearBit(int bit){
           if (bit >= 0 && bit <= 7) {
            registerValue &= ~(1 << bit);
           }
      }
};


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