Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

Give few difference between constructor and method?

959


What happens if an exception is throws from an object's destructor?

1035


What is getkey () in java?

1004


How is a structure different from array ?

1036


Can a static method be overridden in java?

1013


Explain implementation and how is it different from conversion?

1075


When is the garbage collection used in Java?

1197


What is integer valueof?

1098


What do you know about the garbate collector?

1012


How do you escape sequences in java?

1069


What are the 2 types of java programs?

1108


How to Sort Strings which are given in List and display in ascending order without using java api.

4428


How does sublist works in java?

966


Can we have return statement in finally clause? What will happen?

956


What is the purpose of the enableevents() method?

1041