zigzag.go 612 B

1234567891011121314151617181920212223
  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package zigzag implements the zigzag mapping between signed and unsigned
  5. // integers:
  6. // +0 <--> 0
  7. // -1 <--> 1
  8. // +1 <--> 2
  9. // -2 <--> 3
  10. // +2 <--> 4
  11. //
  12. // It is the same format used by protocol buffers. The format is described at
  13. // http://code.google.com/apis/protocolbuffers/docs/encoding.html
  14. package zigzag
  15. func Itou64(i int64) uint64 {
  16. return uint64(i<<1 ^ i>>63)
  17. }
  18. func Utoi64(u uint64) int64 {
  19. return int64(u>>1) ^ -int64(u&1)
  20. }