Skip to content

Instantly share code, notes, and snippets.

@lion137
Last active February 11, 2017 23:20
Show Gist options
  • Select an option

  • Save lion137/2d1967cd73b399a760d0dcf269d6cc47 to your computer and use it in GitHub Desktop.

Select an option

Save lion137/2d1967cd73b399a760d0dcf269d6cc47 to your computer and use it in GitHub Desktop.
Golang tests
//Golang optimizatoin tricks
// copyleft by lion137
// https://lion137.blogspot.co.uk/2017/02/bit-hacks-in-go.html
func average(x uint32, y uint32) uint32 {
return (x & y) + ((x ^ y) >> 1)
}
func iexp(x uint32, n uint32) uint32 {
if (n == 0) {
return 1
}
var p, y uint32
y = 1;
p = x;
for {
if (n & 1 != 0) {y = p*y}
n >>= 1;
if (n == 0) {return y}
p *= p;
}
return 0
}
@lion137
Copy link
Author

lion137 commented Feb 9, 2017

Integer average without overflow and exponentiation by binary decomposition, more here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment