reference:
http://www.geeksforgeeks.org/efficient-way-to-multiply-with-7/
Problem Definition:
Figure out a way to multiply with 7 efficiently.
Solution:
We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n).
Code:
int multiplyBySeven(unsigned int n)
{
/* Note the inner bracket here. This is needed
because precedence of '-' operator is higher
than '<<' */
return ((n<<3) - n);
}
本文介绍了一种使用位操作符高效实现数字乘以7的方法。通过将数字左移3位得到8倍值,然后从该值中减去原始数字,从而快速计算出结果。
911

被折叠的 条评论
为什么被折叠?



