本文共 484 字,大约阅读时间需要 1 分钟。
Figure out a way to multiply with 7 efficiently.
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).
int multiplyBySeven(unsigned int n){ /* Note the inner bracket here. This is needed because precedence of '-' operator is higher than '<<' */ return ((n<<3) - n);}
转载地址:http://fexti.baihongyu.com/