title: Extract data from the frame using C
body: <p>I have a function as follows:</p>
  
  <pre><code>union u_t
  {
    uint16_t u16;
    uint8_t u8[2];
  };
  
  uint16_t Frame2Data(uint8_t *data,uint8_t startBit,uint16_t length)
  {
    uint16_t mask;
    uint8_t start;
    uint8_t firstByte,offset;
    uint8_t numShift;
    union u_t ut;
  
    for(i=0;i&lt;16;i++)
    {
      if(length == i)
      mask|=(1&lt;&lt;i);
    }
  
    firstByte = startBit / 8;
    offset = (firstByte+2) * 8;
    start = startBit + length;
    numShift = offset - start;
    ut.u8[1] = data[firstByte];
    ut.u8[0] = data[firstByte+1];
    return (ut.u16 &gt;&gt; numShift) &amp; mask;
  }
  </code></pre>
  
  <p>The start bit is 46 and length is 7 and the data in 8 bytes is <code>0x00 0x09 0x03 0x84 0x03 0x70 0x02 0xA8</code> has to be passed. I have to extract the data with start bit 46 and upto a length of 7 bits and find out its value. I have a doubt with numbershift and is my function working correctly? It would be great if someone confirms this.Thanks in advance.</p>