News for hackers. Stuff that matters.
Re:Making all your integers positive with zigzag encoding by nynx on Friday November 25, 2022 @02:09PM (#33745753)
Used in the [postcard](https://docs.rs/postcard/latest/postcard/) crate for encoding variably sized signed integers.
Re:Making all your integers positive with zigzag encoding by phoboslab on Friday November 25, 2022 @02:25PM (#33745892)
I first encountered this in the Flac source[1]. They do the bit twiddling a bit differently than in this article:

    uval = (val<<1) ^ (val>>31);
Variations of this have probably been used countless of times in other libraries.

[1] https://github.com/LordJZ/libflac/blob/master/src/libFLAC/bi...

Re:Making all your integers positive with zigzag encoding by jamesmunns on Friday November 25, 2022 @02:58PM (#33746163)
Author of postcard here, I don't remember exactly where I got my bit twiddling example from, lemme see if I have notes from that.

postcard's zigzag encoding matches phoboslab's psuedocode.

Edit, not totally sure, but this wiki page rings a bell, and is probably where I got my impl from: https://en.wikipedia.org/wiki/Variable-length_quantity#Zigza...

Edit 2: I also explain why I do this (it compresses better) in my wire format specification: https://postcard.jamesmunns.com/wire-format.html#signed-inte...