An approach I've used: count the number of leading 1 bits, say `n`. The size of the number is then 2^n bytes (including the leading 1 bits). Take the bits after the first 0 bit as an integer, and add the maximum value (plus one) that can be represented by a number using this encoding in 2^(n-1) bytes. Thus, 0b00000000 -> 0 0b01111111 -> 127 0b10000000 00000000 -> 128 0b10000000 00000001 -> 129 0b10111111 11111111 -> 16511 0b11000000 00000000 00000000 00000000 -> 16512 0b11011111 11111111 11111111 11111111 -> 536887423 etc. This scheme allows any non-negative value to be represented in exactly one way. (Equivalently, used the number of leading 0 bits.)