Skip to main content

Module zigzag

Module zigzag 

Source
Expand description

Zigzag encoding for signed integers.

Zigzag maps signed integers to unsigned ones so that small-magnitude negative values produce small-magnitude varints. Pair these helpers with the varint functions to encode signed values compactly.

The mapping mirrors the one used by Protocol Buffers:

signedunsigned
00
-11
12
-23
24

Functionsยง

decode_i32
Recover the signed i32 from a zigzag-encoded u32.
decode_i64
Recover the signed i64 from a zigzag-encoded u64.
encode_i32
Map a signed i32 to its zigzag-encoded u32.
encode_i64
Map a signed i64 to its zigzag-encoded u64.