64. GPIO State Enum

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

enum class PinState : uint8_t { Low, High };

const char* toString(PinState s) {
    switch (s) {
        case PinState::Low:  return "LOW";
        case PinState::High: return "HIGH";
    }
    return "LOW"; // unreachable fallback
}

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

    PinState s = (x == 0) ? PinState::Low : PinState::High;
    cout << toString(s);
    return 0;
}

Solution Details

  • enum class is strongly typed and scoped, so you must use PinState::Low/High.
  • The explicit underlying type : uint8_t matches typical register bit sizes.
  • toString centralizes text conversion for logging or debugging.

     

Significance for Embedded Developers

  • Replaces “magic numbers” with clear, type-safe states.
  • Keeps API surfaces clean and prevents accidental mixing with integers.
  • Using uint8_t mirrors hardware register widths and saves RAM/ROM.
     
Loading...

Input

0

Expected Output

LOW