reference:
http://www.geeksforgeeks.org/multiply-an-integer-with-3-5/
Problem Definition:
Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.
Examples:
Input: 2
Output: 7
Input: 5
Output: 17 (Ignore the digits after decimal point)
Solution:
This could be done by equation (8*x – x)/2 .
Code:
int multiplyWith3Point5(int x)
{
return ((x<<3) - x)>>1;
}
本文介绍了一种不使用常规算术运算符(如%、/、*)来实现整数与3.5相乘的方法。通过巧妙利用位操作实现了这一目标,提供了一个具体的C/C++代码示例。

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



