Hacker News new | ask | show | jobs
by 8xde0wcNwpslOw 2935 days ago
Bitfields would be, in theory, safer still: no manual bit-twiddling to begin with and also quite straightforward usage. Unfortunately, they are (and I guess will remain) underspecified as far as portability is concerned.

You can, of course, do something similar with member functions, e.g.

  class controller
  {
    unsigned value;
    static constexpr unsigned BTN_A = 0x01;
    ...
  public:
    bool a() const { return value & BTN_A; }
    void a(bool set) { if (set) value |= BTN_A; else value &= ~BTN_A; }
  };
1 comments

This is pretty similar to what I've done in the past