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 / 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


Please Help Members By Posting Answers For Below Questions

What is the use of list in java?

936


How to provide security in java

2197


How does a for loop work?

942


What is the major drawback of internal iteration over external iteration?

968


Is cout buffered?

1112


what is the difference between a threads start() and run() methods? : Java thread

962


What happens if main method is not static?

949


Which access specifier can be used with class ?

944


How will you serialize a singleton class without violating singleton pattern?

1901


Which method must be implemented by all threads?

1194


What is data member in java?

956


What is replacefirst in java?

1024


What does nullpointerexception mean?

1022


Is string is a class in java?

893


What is array pointers ?

979