ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
How to add two numbers with out using Arithmetic , union
operators in java....?
 
But we can use bitwise operators... but how...?
 Question Submitted By :: Chaitanya
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to add two numbers with out using Arithmetic , union operators in java....? But we can use bitwise operators... but how...?
Answer
# 1
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 ?    0 Yes 4 No
Krichait
 
  Re: How to add two numbers with out using Arithmetic , union operators in java....? But we can use bitwise operators... but how...?
Answer
# 2
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 ?    3 Yes 1 No
Sejal
 
 
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
Instead of writing Home, Remote Interfaces if i directly extends EJBObject to bean class what happens? Flextronics1
If you are reviewing the code of your team members, what points will you look at, assuming the performance of the application is not so great KPIT2
what is mean by overriding in which situation we wil use? Atlas-Systems4
How to print nodes of a Binary tree? TCS1
what is generics in jdk1.5? Bally-Technologies1
Why Wait and notify are kept in Object class although they are used only with Thread Class Saksoft2
wahts is mean by thread? HCL14
what is meant by string pooling? Wipro6
Who developed JScript language?  3
How the elements are organized in GridBagLayout?  1
What is protected and friendly?  1
What are batch updates. in jdbc Corent-Technology2
Name the methods that used to get and set the text label displayed by a Buttonobject?  1
What are field variable and local variable?  2
how session will be expired ? Satyam2
What is the argument type of main() method?  2
what modifiers are used with top-level class?  2
can u handle an error if u write Thowable in the catch class lise try { some errorneous code }catch(Throwable e){ ...}  4
What is the difference between pageContext and page implicit objects in jsp? Merrill-Lynch6
Difference between overloading and overridding?  4
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com