See some sample code from EPIP[1]
Also reminiscent of zig zag encoding for ints.
[1]: https://github.com/adnanaziz/EPIJudge/blob/master/epi_judge_...