Skip to main content
2 of 4
added 125 characters in body
jblaze
  • 25
  • 1
  • 11

Slice byte-stream into bit pieces

I receive byte data from a RS232 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 (siwtch 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 forth byte)
value4: 1.00110010.111[00110] => 3737 (add the last bit of the forth byte and cut off the last 5 bits of the fifth byte)

Now the question: how do I realise this from programming side. I'm lost.

Cheers

jblaze
  • 25
  • 1
  • 11