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
Give few difference between constructor and method?
What happens if an exception is throws from an object's destructor?
What is getkey () in java?
How is a structure different from array ?
Can a static method be overridden in java?
Explain implementation and how is it different from conversion?
When is the garbage collection used in Java?
What is integer valueof?
What do you know about the garbate collector?
How do you escape sequences in java?
What are the 2 types of java programs?
How to Sort Strings which are given in List and display in ascending order without using java api.
How does sublist works in java?
Can we have return statement in finally clause? What will happen?
What is the purpose of the enableevents() method?