Use shift and bitwise OR, then convert to a character to get a "byte":
x = chr(a | (b << 1) | (c << 2) | (d << 5))
To unpack this byte again, first convert to an integer, then shift and use bitwise AND:
i = ord(x)
a = i & 1
b = (i >> 1) & 1
c = (i >> 2) & 7
d = (i >> 5) & 7
Explanation: Initially, you have
0000000a
0000000b
00000ccc
00000ddd
The left-shifts give you
0000000a
000000b0
000ccc00
ddd00000
The bitwise OR results in
dddcccba
Converting to a character will convert this to a single byte.
Unpacking: The four different right-shifts result in
dddcccba
0dddcccb
00dddccc
00000ddd
Masking (bitwise AND) with 1
(0b00000001
) or 7
(0b00000111
) results in
0000000a
0000000b
00000ccc
00000ddd
again.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…