#pragma once #include #include //! Convert signed values to unsigned. Convenient for further varint encoding. //! See https://developers.google.com/protocol-buffers/docs/encoding#types for details. template inline auto ZigZagEncode(TSignedInt n) -> std::make_unsigned_t { static_assert(std::is_signed::value && std::is_integral::value, "Expected signed integral type."); auto un = static_cast>(n); return (un << 1) ^ (n >> (CHAR_BIT * sizeof(TSignedInt) - 1)); } template inline auto ZigZagDecode(TUnsignedInt n) -> std::make_signed_t { static_assert(std::is_unsigned::value, "Expected unsigned integral type."); return (n >> 1) ^ -static_cast>(n & 1); }