How do you implement a function to add two integers without utilization of arithmetic +, -, *, and / operators?
int add(int num1, int num2) {
int sum, carry;
do {
sum = num1 ^ num2;
carry = (num1 & num2) << 1;
num1 = sum;
num2 = carry;
} while (num2 != 0);
return num1;
}
How do you implement a function for the subtraction operation without utilization of arithmetic +, -, *, and / operators?
-b can be gotten with bit operations because -b=~b+1.
int subtract(int num1, int num2) {
num2 = add(~num2, 1);
return add(num1, num2);
}
How do you implement a function for the multiplication operation without utilization of arithmetic +, -, *, and / operators?
a×n can be implemented with a left-shift and additions. Since there are O(logn) 1 bits in the binary representation of n, the new solution invokes the add method for O(logn) times.
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;
}
How do you implement a function to divide an integer by another without utilization of arithmetic +, -, *, and / operators?
Array Construction
void multiply(double array1[], double array2[]){
if(array1.length == array2.length && array1.length > 0){
array2[0] = 1;
for(int i = 1; i < array1.length; ++i){
array2[i] = array2[i - 1] * array1[i - 1];
}
int temp = 1;
for(int i = array1.length - 2; i >= 0; --i){
temp *= array1[i + 1];
array2[i] *= temp;
}
}
}
The time complexity of this solution is O(n) obviously
int add(int num1, int num2) {
int sum, carry;
do {
sum = num1 ^ num2;
carry = (num1 & num2) << 1;
num1 = sum;
num2 = carry;
} while (num2 != 0);
return num1;
}
How do you implement a function for the subtraction operation without utilization of arithmetic +, -, *, and / operators?
-b can be gotten with bit operations because -b=~b+1.
int subtract(int num1, int num2) {
num2 = add(~num2, 1);
return add(num1, num2);
}
How do you implement a function for the multiplication operation without utilization of arithmetic +, -, *, and / operators?
a×n can be implemented with a left-shift and additions. Since there are O(logn) 1 bits in the binary representation of n, the new solution invokes the add method for O(logn) times.
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;
}
How do you implement a function to divide an integer by another without utilization of arithmetic +, -, *, and / operators?
Array Construction
void multiply(double array1[], double array2[]){
if(array1.length == array2.length && array1.length > 0){
array2[0] = 1;
for(int i = 1; i < array1.length; ++i){
array2[i] = array2[i - 1] * array1[i - 1];
}
int temp = 1;
for(int i = array1.length - 2; i >= 0; --i){
temp *= array1[i + 1];
array2[i] *= temp;
}
}
}
The time complexity of this solution is O(n) obviously