Created
March 24, 2019 03:25
-
-
Save danielwhite/103410165346ac6c7062f16e7f9045c4 to your computer and use it in GitHub Desktop.
Math Utilities
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
package math | |
// Average returns the floor of the average of two signed integers without risk of overflow. | |
// | |
// See: http://aggregate.org/MAGIC/#Average%20of%20Integers | |
func Average(x, y int64) int64 { | |
return (x & y) + ((x ^ y) >> 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment