Write-1-to-Clear Flag Encapsulation

#include <iostream>
#include <iomanip>
#include <cstdint>

class StatusRegister {
private:
    // Simulates the hardware bus write operation
    // DO NOT MODIFY THIS FUNCTION
    void busWrite(uint32_t val) {
        std::cout << "Write: 0x" 
                  << std::hex << std::uppercase << std::setw(8) << std::setfill('0') 
                  << val << std::endl;
    }

public:
    // TODO: Implement this method to handle Write-1-to-Clear logic.
    // The bit_index is the position (0-31) to clear.
    void clearFlag(int bit_index) {
        // TODO: Calculate the correct mask.
        uint32_t mask = 1<<bit_index;
        // TODO: Call busWrite(mask).
        busWrite(mask);
        // NOTE: Do NOT read the current state. Do NOT use &= ~.
    }
};

int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    StatusRegister driver;

    for (int i = 0; i < N; ++i) {
        int bit_idx;
        std::cin >> bit_idx;
        
        // Clear the specific flag
        driver.clearFlag(bit_idx);
    }

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

3 0 4 31

Expected Output

Write: 0x00000001 Write: 0x00000010 Write: 0x80000000