title: Algorithm for bitmask on CAN messages
body: <p>Using CAN communications with a DBC as database, there are two different ways of encoding integer signals to the 64bit (8 byte) message data: Intel and Motorola.<br>
  To convert the 8 byte of message data to the distinct signals, I first want to use a bitmask to "scope" only the interesting bits and afterwards do some shift operations to get the number the signal is representing.<br>
  Here I am only interested in the bitmask generation:<br>
  Intel (also known as little-endian) is pretty straight forward: you take the startbit and iterate up to <code>startBit + length</code> setting every bit in your 64bit mask to 1 that you come by:</p>
  
  <pre><code>uint64_t generateBitmask(Signal* signal) {
      uint64_t bitmask = 0;
      uint64_t one = 1;
      int startBit = signal-&gt;startbit;
      int endBit = startBit + signal-&gt;length;
  
      if(signal-&gt;byteOrder == INTEL) {
          for(int i = startBit; i &lt; endBit; ++i)
          {
              bitmask |= (one &lt;&lt; i);
          }
      }
      return bitmask;
  }
  </code></pre>
  
  <p>With motorola (big-endian) based numbers it is a bit more complicated:<br>
  since the byteorder (NOT the bitorder) is swapped, the (according to the index) lowest byte might contain "gaps" on the upper bits while the highest (indexed) byte might have a "gaps" on the lower end of the byte (see signal MOTO_20 in image; bits 22/23 and 32/33 are not part of the signal although they are sourrounded by bits that are).</p>
  
  <p>I am searching for a way to calculate this bitmask based on start bit and length of the respective signal. Please find two examples for motorola-based signals layed out in a CAN frame in the image below. Start bits and lengths are given under the image.</p>
  
  <p>The bitmask is supposed to be corresponding to what a CAN bus message layout using a dbc would look like (image as example): every bit of a signal that is marked in the image is supposed to be 1:<br>
  example MOTO_20: <code>00000000 00000000 00000000 11111000 11111111 01111111 00000000 00000000</code><br>
  example MOTO_16: <code>00000000 00000000 00000000 00000000 00000000 00000000 11111111 11111111</code><br>
  The signal MOTO_20 has startbit 34 and length 20<br>
  The signal MOTO_16 has startbit 8 and length 16</p>
  
  <p><a href="https://i.stack.imgur.com/kv002.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kv002.png" alt="Message Layout"></a></p>