Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1524377
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: LINUX

2010-09-07 20:15:11

Binary numeral system (base 2)
 
 

Inherent to digit-by-digit algorithms is a search and test step: find a digit, , when added to the right of a current solution , such that , where is the value for which a root is desired. Expanding, we obtain . The current value of —or, usually, the remainder—can be incrementally updated efficiently when working in binary, as the value of will be a single bit, and the operations needed to compute and can be replaced with faster bit shift operations. This gives rise to simple computer implementations:

 short sqrt(short num) {
        short op = num;
        short res = 0;
        short one = 1 << 14; // The second-to-top bit is set: 1L<<30 for long

        // "one" starts at the highest power of four <= the argument.
        while (one > op)
            one >>= 2;

        while (one != 0) {
            if (op >= res + one) {
                op -= res + one;
                res = (res >> 1) + one;
            }
            else
              res >>= 1;
            one >>= 2;
        }
        return res;
    }
阅读(759) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~