【数据结构和算法实践-位运算-两数之积】
数据结构和算法实践-位运算-两数之积
- 题目
- My Thought
- 代码示例
- JAVA-8
题目
两数之积
My Thought
略
代码示例
JAVA-8
public static int add(int a, int b) {
int result = 0;
while (b != 0) {
result = a ^ b;
b = (a & b) << 1;
a = result;
}
return result;
}
public static int multi(int a, int b) {
int res = 0;
while (b != 0) {
if ((b & 1) != 0) {
res = add(res, a);
}
a <<= 1;
b >>>= 1;
}
return res;
}