How to add two numbers with out using Arithmetic , union
operators in java....?
But we can use bitwise operators... but how...?
Answer Posted / sejal
public class BitWiseOpsExample {
public static int add(int x, int y) {
int xor, and, temp;
and = x & y; /* Obtain the carry bits */
xor = x ^ y; /* resulting bits */
while(and != 0 ) /* stop when carry bits are gone */
{
and <<= 1; /* shifting the carry bits one space */
temp = xor ^ and; /* hold the new xor result bits*/
and &= xor; /* clear the previous carry bits and assign the
new carry bits */
xor = temp; /* resulting bits */
}
return xor; /* final result */
}
public static void main(String[] args) {
System.out.println("Add 4 + 7");
System.out.println(add(4,7));
System.out.println("Add 25 + 25");
System.out.println(add(25,25));
}
}
| Is This Answer Correct ? | 15 Yes | 2 No |
Post New Answer View All Answers
What is javac_g?
What does file separator do in java?
Why is string buffer better than string ?
What is the use of default method in interface in java?
What is adapter in java?
How do you stop a thread in java?
What are the different ways of implementing thread? Which one is more advantageous?
What is the definition of tree ?
How to convert string to char and vice versa?
Name some classes present in java.util.regex package.
why java uses class level type casting ?
How to perform selection sort in java?
Explain the JDB in depth & command line.
How do you get the length of a string in java?
Is space a char?