Re: [protobuf] Zig Zag Encoding (vs two's complement encoding)

2011-01-18 Thread Kenton Varda
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
official.  Your encoding would work equally well, but cannot really be
implemented independently of varint itself, therefore could not have evolved
into existence in this way.

On Fri, Jan 14, 2011 at 12:07 AM, David Srbecky dsrbe...@gmail.com wrote:

 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 complement encoding always
 take the same number of bytes to encode a given number.

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To post to this group, send email to protobuf@googlegroups.com.
 To unsubscribe from this group, send email to
 protobuf+unsubscr...@googlegroups.comprotobuf%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/protobuf?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.



Re: [protobuf] Zig Zag Encoding (vs two's complement encoding)

2011-01-14 Thread David Srbecky
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 complement encoding always
take the same number of bytes to encode a given number.

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.



[protobuf] Zig Zag Encoding

2011-01-13 Thread David Srbecky
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 the design.

Regards,
David Srbecky

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.



Re: [protobuf] Zig Zag Encoding

2011-01-13 Thread Marc Gravell
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 64 bit, as a zig-zag
number shouldn't change (IIRC).

Marc

On 13 January 2011 19:58, David Srbecky dsrbe...@gmail.com wrote:

 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 the design.

 Regards,
 David Srbecky

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To post to this group, send email to protobuf@googlegroups.com.
 To unsubscribe from this group, send email to
 protobuf+unsubscr...@googlegroups.comprotobuf%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/protobuf?hl=en.




-- 
Regards,

Marc

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.