88. Toggle Bitmask

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

class Flags {
private:
    uint8_t bits;
public:
    explicit Flags(uint8_t b) : bits(b) {}

    Flags operator~() const {
        return Flags(static_cast<uint8_t>(~bits));
    }

    uint8_t getBits() const {
        return bits;
    }
};

int main() {
    int val;
    cin >> val;

    Flags f(static_cast<uint8_t>(val));
    Flags toggled = ~f;

    cout << "Input=" << val
         << " Toggled Input=" << static_cast<int>(toggled.getBits());

    return 0;
}

Explanation & Logic Summary:
The ~ operator is overloaded to invert all 8 bits of the internal uint8_t flag and return a new Flags object.
The output explicitly displays both the original input value and its toggled counterpart.

Firmware Relevance & Real-World Context:
Firmware frequently logs or debugs both the original register value and its modified version.
Displaying input and toggled output together mirrors real-world diagnostics when manipulating control registers, GPIO masks, or peripheral configuration flags.


 

Loading...

Input

0

Expected Output

Input=0 Toggled Input=255