The reason ZigZag encoding evolved as it did is because it can be applied as
a separate layer on top of varint encoding. Originally, ZigZag was not
officially part of the protobuf spec, but just something some people did
manually to encode negative numbers efficiently. Eventually we made it
126 is 0111 1110 in binary. We truncate it form the left to
groups of seven: 000 110 and then encode it as usual (least
significant group first): 1 110 0 000. It takes two bytes to
encode, but so does the zig-zag encoding.
In fact, the zig-zag encoding and the two's
Hello,
I am wondering why the integers are encoded as zig zags. It would
seem much more intuitive to me to just truncate the number from the
left. That is, store -1 as 0111, -2 as 0110, 1 as 0001, 2
as 0010 and so on.
I am not proposing to change it. I am just curious about
maybe I'm being slow (it is late here), but what would 126 look like?
You could say ah, pad to the expected length, but then -1 is encoded as
1110 - and as I see it, *that* is
what zip-zag attempts to avoid.
It also solves the problem of switching from 32 to