Truncated Binary Encoding
https://en.wikipedia.org/wiki/Truncated_binary_encoding
k = floor_log2(n);
u = 2 * exp2(k) - n
if x < u
write k
least significant bits of x
else add u
to x
and write k
most significant bits of x
and then write a least significant bit of x
.
- read
k
bits as x
.
- if
u
<= x
then read an additional bit and add it to x
as a least significant bit and substract u
.
BE |
TBE(MSB) |
TBE(LSB) |
|
00 |
0X |
0X |
|
01 |
10 |
10 |
u |
10 |
11 |
11 |
|
BE |
TBE |
|
00 |
00 |
|
01 |
01 |
|
10 |
10 |
|
11 |
11 |
|
n |
|
u |
BE |
TBE |
|
000 |
00 |
|
001 |
01 |
|
010 |
10 |
|
011 |
110 |
u |
100 |
111 |
|
BE |
TBE |
000 |
00 |
001 |
01 |
010 |
100 |
011 |
101 |
100 |
110 |
101 |
111 |
BE |
TBE |
000 |
00 |
001 |
010 |
010 |
011 |
011 |
100 |
100 |
101 |
101 |
110 |
110 |
111 |
BE |
TBE(MSB) |
TBE(LSB) |
0000 |
000X |
000X |
0001 |
001X |
100X |
0010 |
010X |
010X |
0011 |
011X |
110X |
0100 |
100X |
001X |
0101 |
101X |
101X |
0110 |
1100 |
0110 |
0111 |
1101 |
0111 |
1000 |
1110 |
1110 |
1001 |
1111 |
1111 |