end defp do_scan(progress, <<msb::1, rest_7bits::bitstring-(7), rest_binary::binary>>) when msb == 0 do IO.inspect(progress: progress, msb: 0, rest_7bits: rest_7bits, rest_binary: rest_binary) {<<rest_7bits::bitstring, progress::bitstring>>, rest_binary} end defp do_scan(progress, <<msb::1, rest_7bits::bitstring-(7), rest_binary::binary>>) when msb == 1 do IO.inspect(progress: progress, msb: 1, rest_7bits: rest_7bits, rest_binary: rest_binary) do_scan(<<rest_7bits::bitstring, progress::bitstring>>, rest_binary) end end binary = :binary.encode_unsigned(0b10010110_10000001_00000011) MsbBinary.scan(binary) [progress: "", msb: 1, rest_7bits: <<22::size(7)>>, rest_binary: <<129, 3, 5, 64>>] [progress: <<22::size(7)>>, msb: 1, rest_7bits: <<1::size(7)>>, rest_binary: <<3, 5, 64>>] [progress: <<2, 22::size(6)>>, msb: 0, rest_7bits: <<3::size(7)>>, rest_binary: <<5, 64>>] {<<6, 4, 22::size(5)>>, <<5, 64>>} # Result 6 . 11