CodedInputStream.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. 
  2. namespace Google.ProtocolBuffers {
  3. public sealed class CodedInputStream {
  4. /// <summary>
  5. /// Decode a 32-bit value with ZigZag encoding.
  6. /// </summary>
  7. /// <remarks>
  8. /// ZigZag encodes signed integers into values that can be efficiently
  9. /// encoded with varint. (Otherwise, negative values must be
  10. /// sign-extended to 64 bits to be varint encoded, thus always taking
  11. /// 10 bytes on the wire.)
  12. /// </remarks>
  13. public static int DecodeZigZag32(uint n) {
  14. return (int)(n >> 1) ^ -(int)(n & 1);
  15. }
  16. /// <summary>
  17. /// Decode a 32-bit value with ZigZag encoding.
  18. /// </summary>
  19. /// <remarks>
  20. /// ZigZag encodes signed integers into values that can be efficiently
  21. /// encoded with varint. (Otherwise, negative values must be
  22. /// sign-extended to 64 bits to be varint encoded, thus always taking
  23. /// 10 bytes on the wire.)
  24. /// </remarks>
  25. public static long DecodeZigZag64(ulong n) {
  26. return (long)(n >> 1) ^ -(long)(n & 1);
  27. }
  28. }
  29. }