写一个函数,求两个整数之和,要求在函数体内不得使用+、-、x、/四则运算符号。
看来只能使用位运算了。
int subtract(int num1, int num2) {
num2 = add(~num2, 1);
return add(num1, num2);
}
int multiply(int num1, int num2) {
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
while (num1 > 0) {
if ((num1 & 0x1) != 0) {
result = add(result, num2);
}
num2 = num2 << 1;
num1 = num1 >> 1;
}
if (minus)
result = add(~result, 1);
return result;
}
int divide(int num1, int num2) {
if (num2 == 0)
throw new ArithmeticException("num2 is zero.");
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
for (int i = 0; i < 32; i=add(i, 1)) {
result = result << 1;
if ((num1 >> (31 - i)) >= num2) {
num1 = subtract(num1, num2 << (31 - i));
result = add(result, 1);
}
}
if (minus)
result = add(~result, 1);
return result;
}
int divide(int num1, int num2) {
if (num2 == 0)
throw new ArithmeticException("num2 is zero.");
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
for (int i = 0; i < 32; i=add(i, 1)) {
result = result << 1;
if ((num1 >> (31 - i)) >= num2) {
num1 = subtract(num1, num2 << (31 - i));
result = add(result, 1);
}
}
if (minus)
result = add(~result, 1);
return result;
}
Read full article from c++ - How to add two numbers without using ++ or + or another arithmetic operator - Stack Overflow
看来只能使用位运算了。
private
static
int
add(
int
num1,
int
num2){
int
sum =
0
;
int
carry =
0
;
do
{
sum = num1 ^ num2; // no need use sum
carry = (num1 & num2)<<
1
;
num1 = sum;
num2 = carry;
}
while
(num2 !=
0
);
return
num1;
}
int
Add(
int
x,
int
y)
{
// Iterate till there is no carry
while
(y != 0)
{
// carry now contains common set bits of x and y
int
carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x gives the required sum
y = carry << 1;
}
return
x;
}
int
Add(
int
x,
int
y)
{
if
(y == 0)
return
x;
else
return
Add( x ^ y, (x & y) << 1);
}
int subtract(int num1, int num2) {
num2 = add(~num2, 1);
return add(num1, num2);
}
int multiply(int num1, int num2) {
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
while (num1 > 0) {
if ((num1 & 0x1) != 0) {
result = add(result, num2);
}
num2 = num2 << 1;
num1 = num1 >> 1;
}
if (minus)
result = add(~result, 1);
return result;
}
int multiply(int x, int y) {
int result = 0;
if(x < 0 && y < 0) {
return multiply(negate(x), negate(y));
}
if(x >= 0 && y < 0) {
return multiply(y, x);
}
while(y > 0) {
if(is_even(y)) {
x = multiply_by_two(x);
y = divide_by_two(y);
} else {
result = add(result, x);
y = add(y, -1);
}
}
return result;
}
From Coding Interviews: Questions, Analysis & Solutionsint divide(int num1, int num2) {
if (num2 == 0)
throw new ArithmeticException("num2 is zero.");
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
for (int i = 0; i < 32; i=add(i, 1)) {
result = result << 1;
if ((num1 >> (31 - i)) >= num2) {
num1 = subtract(num1, num2 << (31 - i));
result = add(result, 1);
}
}
if (minus)
result = add(~result, 1);
return result;
}
int divide(int num1, int num2) {
if (num2 == 0)
throw new ArithmeticException("num2 is zero.");
boolean minus = false;
if ((num1 < 0 && num2 > 0) || (num1 > 0 && num2 < 0))
minus = true;
if (num1 < 0)
num1 = add(~num1, 1);
if (num2 < 0)
num2 = add(~num2, 1);
int result = 0;
for (int i = 0; i < 32; i=add(i, 1)) {
result = result << 1;
if ((num1 >> (31 - i)) >= num2) {
num1 = subtract(num1, num2 << (31 - i));
result = add(result, 1);
}
}
if (minus)
result = add(~result, 1);
return result;
}
int add(int x, int y) {
int carry = 0;
int result = 0;
int i;
for(i = 0; i < 32; ++i) {
int a = (x >> i) & 1;
int b = (y >> i) & 1;
result |= ((a ^ b) ^ carry) << i;
carry = (a & b) | (b & carry) | (carry & a);
}
return result;
}
int negate(int x) {
return add(~x, 1);
}
int subtract(int x, int y) {
return add(x, negate(y));
}
int is_even(int n) {
return !(n & 1);
}
int divide_by_two(int n) {
return n >> 1;
}
int multiply_by_two(int n) {
return n << 1;
}
Read full article from c++ - How to add two numbers without using ++ or + or another arithmetic operator - Stack Overflow