Integer mean without casing an overflow and exponenation by binary decomposition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 } |
Average:
1 2 3 | func average(x uint32, y uint32) uint32 { return (x & y) + ((x ^ y) >> 1) } |
This time the adventage is not speed, but the fact, that we have the average without overflow (works, for ex., for:, 2^32 - 1 and 2^32 - 1).
That's it, code also on github, till the next time!
3 comments:
I tried same average function in C but i do not understand how is that faster. In assembler it seems that time would be similar if not same. See here for my results https://godbolt.org/g/CEmy2E
Thanks for bringing this, my bad, I did't state it clear ('ve updated the post anyway), the trick in the average function is, that it won't overflow not speed.
Ok now I get it. The glitch in the division cause the overflow like here:
http://ideone.com/Xpt8dN
Post a Comment