Last active
February 11, 2017 23:20
-
-
Save lion137/2d1967cd73b399a760d0dcf269d6cc47 to your computer and use it in GitHub Desktop.
Golang tests
This file contains hidden or 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
| //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 | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Integer average without overflow and exponentiation by binary decomposition, more here