Register Bitwise Operations

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

class Reg32{
    private:
        uint32_t value;
    
    public:
        Reg32(uint32_t x): value(x){}

        Reg32& operator|=(uint32_t x){
            value |= x;
            return *this;   //return the object; itself not the value
        }

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

        operator uint32_t(){    // init this conversion operator so that when the compiler expects a uint32_t but instead has a Reg32_t object it calls this function
            return value;
        }
};


int main() {
    uint32_t init, orVal, andVal;
    cin >> init >> orVal >> andVal;

    Reg32 reg(init);

    reg |= orVal;     // bitwise OR
    reg &= andVal;    // bitwise AND

    cout << (uint32_t)reg;

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

8 5 14

Expected Output

12