75. Register Bitwise Operations

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

class Reg32 {
private:
    uint32_t value;

public:
    // Initialize register with a 32-bit value
    Reg32(uint32_t v) : value(v) {}

    // Bitwise OR assignment
    Reg32& operator|=(uint32_t x) {
        value |= x;
        return *this;
    }

    // Bitwise AND assignment
    Reg32& operator&=(uint32_t x) {
        value &= x;
        return *this;
    }

    // Conversion operator for output
    operator uint32_t() const {
        return value;
    }
};

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

    Reg32 reg(init);

    reg |= orVal;
    reg &= andVal;

    cout << (uint32_t)reg;

    return 0;
}

Explanation & Logic Summary:

  • operator|= modifies the register by setting bits present in the input mask.
  • operator&= masks the register, clearing bits not present in the input mask.
  • The conversion operator allows the register to behave like a raw uint32_t when printed or assigned.
  • All operations are performed using fixed-width integer types suitable for firmware development.

Firmware Relevance & Real-World Context:

This problem reflects real-world embedded scenarios such as:

  • Hardware register manipulation
  • Bit masking and flag control
  • Peripheral configuration
  • Status and control register updates
  • Low-level protocol handling

It reinforces operator overloading, bitwise logic, and type safety, which are essential skills for Embedded C++ and firmware developers.

 

 

 

 

Loading...

Input

8 5 14

Expected Output

12