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

But we can use bitwise operators... but how...?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What are strings in physics?

749


What are multiple inheritances?

812


Whats the difference between notify() and notifyall()?

775


How do you sort words in java?

703


How do you override a method?

730


What is factor r?

723


How can we make string upper case or lower case?

775


What is a parameter in simple terms?

740


What are the differences between the constructors and methods?

798


What is difference between classpath and path variables in java?

847


EDS (Electronic Data Systems India Pvt Ltd) at Chennai on 16-12-2006.

8883


What is the difference between choice and list?

816


What is the difference between static and global variables and also define what are volatile variables?

782


What is a singleton puppy?

723


What is the difference between int and integer in java?

752