Skip to content

Instantly share code, notes, and snippets.

@xinmyname
Created December 7, 2022 16:35
Show Gist options
  • Save xinmyname/952f9e3a0baa841f9d3ec065e9568972 to your computer and use it in GitHub Desktop.
Save xinmyname/952f9e3a0baa841f9d3ec065e9568972 to your computer and use it in GitHub Desktop.
Fast integer midpoint
int MidRoundUp(int x, int y) {
return (x | y) - ((x ^ y) >> 1);
}
int MidRoundDown(int x, int y) {
return ((x ^ y) >> 1) + (x & y);
}
/* Source: https://lemire.me/blog/2022/12/06/fast-midpoint-between-two-integers-without-overflow/ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment