csr*_*gye 5 bit-shift square-root
我正在通过位移来研究快速平方根算法。我被维基百科的代码困住了。
short isqrt(short num) {
short res = 0;
short bit = 1 << 14; // The second-to-top bit is set: 1L<<30 for long
// "bit" starts at the highest power of four <= the argument.
while (bit > num)
bit >>= 2;
while (bit != 0) {
if (num >= res + bit) {
num -= res + bit;
res = (res >> 1) + bit;
}
else
res >>= 1;
bit >>= 2;
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
我知道它可以产生正确的结果,但它是如何做到的?我对这句话特别迷惑,res = (res >> 1) + bit; 为什么这里要除以2?任何人都可以对此有所了解吗?谢谢!
| 归档时间: |
|
| 查看次数: |
10086 次 |
| 最近记录: |