user11150: I receive byte data from a RS-232 device with Serial.read();. Now I need to slice this down in bit-pieces:

value1: 15bits
value2: 7bits
value3: 9bits
value4: 12bits
and so on

Data from the stream (switch them around for better understanding):

Byte 1: 00101100 => 00110100 (least significant byte first)
Byte 2: 10011011 => 11011001
Byte 3: 00110011 => 11001100
Byte 4: 11101000 => 00010111
Byte 5: 01001100 => 00110010
Byte 6: 01100111 => 11100110

Now the least significant is first.

value1: 00110100.1101100[1] => 6956
Need to cut of the last bit of the second byte
value2: 1.110011[00] => 103
Add the last bit of the second byte and cut off the last two bits of the third byte
value3: 00.0001011[1] => 416
Add the last two bits of the third byte and cut off the last bit of the fourth byte
value4: 1.00110010.111[00110] => 3737
Add the last bit of the fourth byte and cut off the last 5 bits of the fifth byte

Now the question: How do I realize this from programming side. I'm lost.

user972: Note: This is an answer that OP originally edited into his question.

I gather the data into an array of bytes data[125].
Then I gather the relevant bytes per value and stack them together:
long var = (data[4]<<8) + (data[3]);

I shift them so the relevant bytes start first
var=var>>3;

Afterwards I mask them with the RIGHT mask
var=var & 0xFF;

For the example in my question:
long var = (data[1]<<8) + (data[0]); // I need two bytes  
var=var>>1; // shift them to the right one bit  
var=var & 0x7FFF; // mask it with "0111 1111 1111 1111"


11150: <p>I receive byte data from a RS-232 device with <code>Serial.read();</code>. Now I need to slice this down in bit-pieces:</p>

<ul>
<li>value1: 15bits</li>
<li>value2: 7bits</li>
<li>value3: 9bits</li>
<li>value4: 12bits</li>
<li>and so on</li>
</ul>

<p>Data from the stream (switch them around for better understanding):</p>

<ul>
<li>Byte 1: 00101100 => 00110100 (least significant byte first)</li>
<li>Byte 2: 10011011 => 11011001</li>
<li>Byte 3: 00110011 => 11001100</li>
<li>Byte 4: 11101000 => 00010111</li>
<li>Byte 5: 01001100 => 00110010</li>
<li>Byte 6: 01100111 => 11100110</li>
</ul>

<p>Now the least significant is first.</p>

<ul>
<li>value1: <code>00110100.1101100[1]</code> => 6956<br>
Need to cut of the last bit of the second byte</li>
<li>value2: <code>1.110011[00]</code> => 103<br>
Add the last bit of the second byte and cut off the last two bits of the third byte</li>
<li>value3: <code>00.0001011[1]</code> => 416<br>
Add the last two bits of the third byte and cut off the last bit of the fourth byte</li>
<li>value4: <code>1.00110010.111[00110]</code> => 3737<br>
Add the last bit of the fourth byte and cut off the last 5 bits of the fifth byte</li>
</ul>

<p>Now the question: How do I realize this from programming side. I'm lost.</p>
 972: <p><strong>Note: This is an answer that OP originally edited into his question.</strong></p>

<hr>

<p>I gather the data into an array of bytes <code>data[125]</code>.</p>

<p>Then I gather the relevant bytes per value and stack them together:</p>

<pre><code>long var = (data[4]&lt;&lt;8) + (data[3]);
</code></pre>

<p>I shift them so the relevant bytes start first</p>

<pre><code>var=var&gt;&gt;3;
</code></pre>

<p>Afterwards I mask them with the RIGHT mask</p>

<pre><code>var=var &amp; 0xFF;
</code></pre>

<p>For the example in my question:</p>

<pre><code>long var = (data[1]&lt;&lt;8) + (data[0]); // I need two bytes  
var=var&gt;&gt;1; // shift them to the right one bit  
var=var &amp; 0x7FFF; // mask it with "0111 1111 1111 1111"
</code></pre>
