Basic
We compressing integers, algorithms like Elias Gamma, are based on strictly positive integers. It is possible to also encode negative integer with a simple bijectionWe want to encode X. X can be zero, positive or negative, we will first transform X into a new (and positive) integer :
if X >=0 : f(X)= 2*X+1
if X <0 : f(X)= -2*X
Examples with gamma encoding
x=6f(X) = 12 + 1 = 13x=-6
gamma(13) = 000 1101
f(-6) = 12
gamma(12) = 000 1100
We can notice that the gamma codes of 6 and -6 are almost the same, only the last bit is different and give use the sign (usefull to decode)
x=5
f(X) = 10 + 1 = 11x=-5
gamma(11) = 000 1011
f(-5) = 10
gamma(12) = 000 1010