Created
December 7, 2022 16:35
-
-
Save xinmyname/952f9e3a0baa841f9d3ec065e9568972 to your computer and use it in GitHub Desktop.
Fast integer midpoint
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
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