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...?

Answers were Sorted based on User's Feedback



How to add two numbers with out using Arithmetic , union operators in java....? But we can use ..

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

How to add two numbers with out using Arithmetic , union operators in java....? But we can use ..

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

How to add two numbers with out using Arithmetic , union operators in java....? But we can use ..

Answer / midhula

public int addtwo(int a,int b)
{
if(b==0)
return a;

int sum = a ^ b; // ^ will be 1 if both operands are
//different otherwise 0.
int carry = (a & b) <<1; //& will be 1 if both operands are
//1 otherwise 0

return addtwo(sum,carry);
}

Is This Answer Correct ?    1 Yes 1 No

How to add two numbers with out using Arithmetic , union operators in java....? But we can use ..

Answer / krichait

Hello... this is the way to write......

// Add two ints using bitwise operators

public class ArithWithBitOps {

//------------------------------------------------------------------------------
// Second attempt - Go bit by bit right to left doing
binary arith
static int carry = 0; // c=1 for 1+1

static int add2(int a, int b) {
int sum = 0; // get sum here
int mask = 1; // shifting mask used
to test each bit

while(mask != 0) {
int ta = a & mask; // get next bit
to add
int tb = b & mask; // get next bit
to add
int bsum = bitAdd2(ta, tb, mask); // add the bits
& set carry
if(Testing) System.out.print("ta=" + ta + ", tb=" +
tb + ", bsum=" + bsum
+ ", carry=" + carry + ", b4sum="
+ sum);
sum = (sum | bsum); // OR in the
results
if(Testing) System.out.println(", aft sum=" + sum);
mask = mask << 1; // move to next bit
} // end while() thru bits

if(carry > 0) System.err.println(">>>>>>>>>>>>losing
carry");
return sum;
} // end add2()

//----------------------------------------------------------
// Add two selected bits in x and y & set carry if carry
static int bitAdd2(int x, int y, int bsel) {
// System.out.println("bitAdd2 of " + x + " " + y);
int tx = x & bsel;
int ty = y & bsel;
if(carry == 1) { // Have carry ?
if((tx & ty) != 0) {
carry = 1;
return bsel; // 1 + 1 + c=1 -> 1 + c=1
}else if((tx ^ ty) != 0) {
carry = 1;
return 0; // 1 + 0 + c=1 -> 0 + c=1
}else {
carry = 0;
return bsel; // 0 + 0 + c=1 -> 1 + c=0
}
}else if (carry == 0) { // no carry
if((tx & ty) > 0) {
carry = 1;
return 0; // 1 + 1 + c=0 -> 0 + c=1
}else if((tx ^ ty) != 0) {
carry = 0;
return bsel; // 1 + 0 + c=0 -> 1 + c=0
}else {
System.out.println("(tx ^ ty)=" + (tx ^ ty));
carry = 0;
return 0; // 0 + 0 + c=0 -> 0 + c=0
}
}else {
System.err.println("Invalid carry= " + carry);
return 0;
}
} // end bitAdd2()

//------------------------------------------------
// Test the above
public static void main(String[] args) {
// The numbers to add
int x = -24;
int y = 15;

if(Testing) System.out.println("Max int=" +
Integer.MAX_VALUE //Max int=2147483647
+ " " + Integer.toHexString(-2) //-2=fffffffe
+ " " + 0x80000000); //-2147483648
int sum = add2(x, y);
System.out.println("Final sum= " + sum + " vs " + (x +
y) + " " + Integer.toHexString(sum));
//Final sum= -9 vs -9 fffffff7
} // end main()

static final boolean Testing = false; // global flag for
debug output
}

Is This Answer Correct ?    2 Yes 4 No

Post New Answer

More Core Java Interview Questions

How do you use, call, and access a non-static method in Java?

2 Answers  


What is ph and buffers?

0 Answers  


What are 5 boolean operators?

0 Answers  


could you run the java program without main method?

6 Answers  


In Java, what types of classes perform inheritance?

2 Answers  


What is final method in java?

0 Answers  


Can we override a variable in java?

0 Answers  


How to print nodes of a Binary tree?

0 Answers  


What is difference between Heap and Stack Memory?

0 Answers  


What is nested top-level class?

0 Answers  


What is difference between fileinputstream and filereader in java?

0 Answers  


Can we override the main method?

4 Answers  


Categories