title: How to encode 3 integer values into an uint16_t using bitwise operations?
body: <p>I want to store 3 unsigned integer values into an <code>uint16_t</code> variable by doing bitwise operations and read them back using bitwise operations. Following is my program to do that:</p>
  <p><strong>Code:</strong></p>
  <pre><code>#include &lt;iostream&gt;
  
  uint16_t Write(unsigned int iVal1, unsigned int iVal2, unsigned int iVal3) {
      // iVal1 should go into the first 8 bits [iVal1 value ranges from 0 to 173]
      // iVal2 should go into the 6 bits after that [iVal2 value ranges from 0 to 63]
      // iVal3 should go into the 2 bits after that [iVal3 value ranges from 0 to 3]
      // Is the below way of writing the bits correct?
      return (static_cast&lt;uint16_t&gt;(iVal1)&lt;&lt;8) + (static_cast&lt;uint16_t&gt;(iVal2)&lt;&lt;6) + (static_cast&lt;uint16_t&gt;(iVal3)&lt;&lt;2);
  }
  
  unsigned int ReadVal1(const uint16_t theNumber) {
      // ival1 is the first 8 bits
      uint16_t check1 = 255;
      return (theNumber&gt;&gt;8)&amp;check1;
  }
  
  unsigned int ReadVal2(const uint16_t theNumber) {
      // ival2 is the 6 bits after that
      uint16_t check2 = 63;
      return (theNumber&gt;&gt;3)&amp;check2;
  }
  
  unsigned int ReadVal3(const uint16_t theNumber) {
      // ival3 is the last 2 bits
      uint16_t check3 = 3;
      return (theNumber&gt;&gt;1)&amp;check3;
  }
  
  int main() {
      std::cout &lt;&lt; &quot;Main started&quot; &lt;&lt; std::endl;
  
      unsigned int iVal1 = 191;
      unsigned int iVal2 = 28;
      unsigned int iVal3 = 3;
  
      const uint16_t theNumber = Write(iVal1, iVal2, iVal3);
  
      std::cout &lt;&lt; &quot;The first 8 bits contain the number: &quot; &lt;&lt; ReadVal1(theNumber) &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;Then after 6 bits contain the number: &quot; &lt;&lt; ReadVal2(theNumber) &lt;&lt; std::endl;
      std::cout &lt;&lt; &quot;Then after 2 bits contain the number: &quot; &lt;&lt; ReadVal3(theNumber) &lt;&lt; std::endl;
  }
  </code></pre>
  <p>In above program following are the ranges of the 3 unsigned integers that need to be encoded.</p>
  <pre><code>`iVal1` ranges from `0 to 173`. So its well within 8 bits.
  `iVal2` ranges from `0 to 63`. So its well within 6 bits.
  `iVal3` ranges from `0 to 3`. So its well within 2 bits.
  </code></pre>
  <p><strong>Question:</strong><br />
  I think that the way I am writing the values inside the function <code>Write</code> is wrong. What is the correct way?</p>
  <p><em>Primarily, I am looking for a good explanation</em> of how the encoding using bitwise operation works especially in the context of the goal of my program above.</p>
  <p>I believe that my way of reading the values in the functions <code>ReadVal1</code>, <code>ReadVal2</code> and <code>ReadVal3</code> is correct. I have figured out the trick of how to read back the values which seems easy. But, I could not well understand the logic of how to encode the values correctly using bitwise operations.</p>
  <p><strong>C++ compiler:</strong><br />
  I am using a <strong>C++11</strong> compiler</p>