Many examples that explain how bit syntax works only demonstrate byte-aligned situations,
I wrote some simple examples about the non-byte-aligned situations, which helped me a lot.
I hope these examples can help people who are not familiar with bit syntax or people who are teaching Erlang to others.
Example 1
<<7:9, 0:23>>.
%> <<3,128,0,0>>
BYTE 0 BYTE 1 BYTE 2 BYTE 3
M L M L M L M L
+--------+ +--------+ +--------+ +--------+
|********| |* | | | | |
+--------+ +--------+ +--------+ +--------+
0 0000000 00000000 00000000
0000011 1
^---------^
Example 2
<<7:9/little, 0:23>>.
%> <<7,0,0,0>>
BYTE 0 BYTE 1 BYTE 2 BYTE 3
M L M L M L M L
+--------+ +--------+ +--------+ +--------+
|********| |* | | | | |
+--------+ +--------+ +--------+ +--------+
00000000 00000000 00000000
00000111
^------^
Example 3
<<7:9, 7:9, 0: 14>>.
%> <<3,129,192,0>>
BYTE 0 BYTE 1 BYTE 2 BYTE 3
M L M L M L M L
+--------+ +--------+ +--------+ +--------+
| | | *******| |** | | |
+--------+ +--------+ +--------+ +--------+
00000011 10 000000 00000000
000001 11
^---------^
Example 4
<<7:9, 7:9/little, 0: 14>>.
%> <<3,131,128,0>>
BYTE 0 BYTE 1 BYTE 2 BYTE 3
M L M L M L M L
+--------+ +--------+ +--------+ +--------+
| | | *******| |** | | |
+--------+ +--------+ +--------+ +--------+
00000011 1 0000000 00000000
0000011 1
^---------^