Skip to content

Instantly share code, notes, and snippets.

@19h
Last active November 12, 2018 20:08
Show Gist options
  • Save 19h/7720542 to your computer and use it in GitHub Desktop.
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.
var pair = function (x, y) {
return x << 16 & 0xffff0000 | y & 0x0000ffff;
};
var depair = function (p) {
return [p >> 16 & 0xFFFF, p & 0xFFFF]
};
@19h
Copy link
Author

19h commented Nov 30, 2013

Example:

pair(1920,1080) // 125830200
depair(125830200) // [1920, 1080]

@19h
Copy link
Author

19h commented Dec 3, 2013

For every pair (x + y) & 0xFFFF === (x + y) pairs may be chained in the form of a tree in order to pair multiple values.

@d4tocchini
Copy link

@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