How to add two numbers with out using Arithmetic , union
operators in java....?
But we can use bitwise operators... but how...?
Answer Posted / mathi
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 ? | 1 Yes | 0 No |
Post New Answer View All Answers
Can we execute java program without main method?
Explain the advantages of packages in java?
Explain about the performance aspects of core java?
What is the do while loop syntax?
How do I compare two strings in word in java?
What is native method in java?
why are wait(), notify() and notifyall() methods defined in the object class? : Java thread
Can a constructor be private and how are this() and super() method used with constructor?
Explain the difference between jdk, jre, and jvm?
How do you implement tree mirroring in java?
What are the advantages of exception handling in java?
What data type is string java?
What are thread groups?
What is core java called?
Is arraylist an object in java?