Last active
November 12, 2018 20:08
-
-
Save 19h/7720542 to your computer and use it in GitHub Desktop.
Pairing Function for Javascript. Combines two numbers into one; used for compression of analytics data where two coordinates of mouse-clicks are combined into a single integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pair = function (x, y) { | |
return x << 16 & 0xffff0000 | y & 0x0000ffff; | |
}; | |
var depair = function (p) { | |
return [p >> 16 & 0xFFFF, p & 0xFFFF] | |
}; |
@KenanSulayman, how would you chain a set of pairs?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For every pair
(x + y) & 0xFFFF === (x + y)
pairs may be chained in the form of a tree in order to pair multiple values.