ZigZag encoding maps signed integers to unsigned integers so that numbers with a small absolute value (for instance, -1) have a small variant encoded value too. It does this in a way that "zig-zags" back and forth through the positive and negative integers, so that -1 is encoded as 1, 1 is encoded as 2, -2 is encoded as 3, and so on
Questions tagged [zigzag-encoding]
10 questions
31
votes
6 answers
Zig Zag Decoding
In the google protocol buffers encoding overview, they introduce something called "Zig Zag Encoding", this takes signed numbers, which have a small magnitude, and creates a series of unsigned numbers which have a small magnitude.
For example
Encoded…

Martin
- 12,469
- 13
- 64
- 128
24
votes
3 answers
Google Protocol Buffers: ZigZag Encoding
From "Signed Types" on Encoding - Protocol Buffers - Google Code:
ZigZag encoding maps signed integers to unsigned integers so that numbers with a small absolute value (for instance, -1) have a small varint encoded value too. It does this in a way…

Thanatos
- 42,585
- 14
- 91
- 146
21
votes
1 answer
What's the reason behind ZigZag encoding in Protocol Buffers and Avro?
ZigZag requires a lot of overhead to write/read numbers. Actually I was stunned to see that it doesn't just write int/long values as they are, but does a lot of additional scrambling. There's even a loop…

Endrju
- 2,354
- 16
- 23
3
votes
0 answers
How to reorder/reshape N-D matrix in zigzag manner?
I would like to sort cells of tensor in this way
Note, that each cell is not a single value, but a stack of values (DCT components). So, the dimensions of original tensor is not 2D.

Dims
- 47,675
- 117
- 331
- 600
3
votes
2 answers
Bitwise operations, wrong result in Dart2Js
I'm doing ZigZag encoding on 32bit integers with Dart. This is the source code that I'm using:
int _encodeZigZag(int instance) => (instance << 1) ^ (instance >> 31);
int _decodeZigZag(int instance) => (instance >> 1) ^ (-(instance & 1));
The code…

Fox32
- 13,126
- 9
- 50
- 71
2
votes
0 answers
Who should be credited for VLQ and zig-zag encoding?
I have designed a (rather simple) network protocol which relies heavily on VLQ-coded integers and zig-zag coding (as described by the Protobuf). I learned about these techniques on the Internet and they felt quite obvious to me. However, now I want…

firegurafiku
- 3,017
- 1
- 28
- 37
1
vote
2 answers
Function to Create a Zigzag Array in Python
I'm trying to create an array of positive and negative integers, representing distances north and south of a location - I need to see the elements of the array in zigzag order.
This means that the largest member appears first, the smallest member…

HappyHands31
- 4,001
- 17
- 59
- 109
0
votes
0 answers
Keep getting the wrong answer even though the expected output = output (HackerRank) (Python)
I'm on my third day in HackerRank doing the zigzag sequence and although my output is the same as the expected output, it keeps saying I got the wrong answer:
Had the same problem with my mock test earlier and am extremely frustrated by this since I…

niconico
- 9
- 2
0
votes
0 answers
Is there a faster way to do ZigZag encoding in JavaScript that works with large numbers?
I am attempting to perform ZigZag encoding for numbers in JavaScript that are within the safe bounds of JavaScript integers
These functions seem to work up to 2^30 - 1 only because JavaScript bit-wise operators only work with 32 bit…

Ryan Peschel
- 11,087
- 19
- 74
- 136
0
votes
1 answer
ZigZag decode/encode in Java
I'm looking for some library which can provide with the functions that can help deconding zig-zag encoded byte array into 2's complement long/int and back.
Since ZigZag is used in protobuf I expected that guava has something for it, but googling…

Some Name
- 8,555
- 5
- 27
- 77