Skip to content

Instantly share code, notes, and snippets.

@pigmon93
Last active December 13, 2016 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigmon93/6e03bbc4e2920ac2ef94a45c2fcb7328 to your computer and use it in GitHub Desktop.
Save pigmon93/6e03bbc4e2920ac2ef94a45c2fcb7328 to your computer and use it in GitHub Desktop.
Zigzag Encoding

for int32

(n << 1) ^ (n >> 31)

for int64

(n << 1) ^ (n >> 63)

example)

Signed Original Encoded As
0 0
-1 1
1 2
-2 3
2147483647 4294967294
-2147483648 4294967295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment