Skip to content

Instantly share code, notes, and snippets.

@danielwhite
Created March 24, 2019 03:25
Show Gist options
  • Save danielwhite/103410165346ac6c7062f16e7f9045c4 to your computer and use it in GitHub Desktop.
Save danielwhite/103410165346ac6c7062f16e7f9045c4 to your computer and use it in GitHub Desktop.
Math Utilities
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