title: How do I do bit operations on a struct?
body: <p>I have a bitfield struct on which I want to perform bitwise operations using masks.
  I want to know the simplest and most efficient way to do this.
  I have tried using my conversion operator (which seems an inefficient way of doing the &amp; operation on two structs) but I get error C2440: 'type cast' : cannot convert from 'const test::dtType' to 'char'. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called</p>
  
  <pre><code>class test
  {
     public:
      test() : startTime(0), endTime(5,23) {}
      ~test();
  
      struct dtType {
          // inline constructors with initialisation lists
          dtType() {dtType(0);}
          dtType(byte z) {dtType(z,z);}
          dtType(byte n,byte h) : mins(n), hrs(h){}
  
          // inline overloaded operator functions
          operator char() {return mins + hrs&lt;&lt;3;}; // allow casting the struct as a char
  
          // All I want to do is return (this &amp; hrsMask == date &amp; hrsMask)
          // I know that in this trivial case I can do return (hrs == date.hrs) but I have simplified it for this example.
          bool operator== (dtType date){return (char(this) &amp; char(hrsMask)) == (char(date) &amp; char(hrsMask));};
  
          // data members
          unsigned mins: 3; // 10's of mins
          unsigned hrs: 5; // 8 bits
      };
      const static dtType hrsMask; // initialised outside the declaraion, since a static is not associated with an individual object.
      dtType startTime; // initialised by test() in its initialisation list
      dtType endTime; // initialised by test() in its initialisation list
  };
  
  // Typically in a source file, but can be in the header.
  const test::dtType hrsMask(0,31);
  </code></pre>
  
  <p>I have tried using void pointers to do the bitwise operation. It compiles, but I haven't tested it.</p>
  
  <pre><code>bool test::dtType::operator== (dtType date){
      const void * longThisDT = this;
      const void * longThatDT = &amp; date;
      const void * longMaskDT = &amp; hrsMask;
      return (*((long *)longThisDT) &amp;  *((long *)longMaskDT) == *((long *)longThatDT) &amp;  *((long *)longMaskDT));
  };
  </code></pre>
  
  <p>Is this about as efficient as we can get? It involves three extra pointers, when all I really need is a cast to long.</p>