Results 1 to 10 of 10
  1. #1
    krichait is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Thumbs up How to write....**

    write a program of any adding or subtracting or multiplication "WITHOUT"
    using Arithmetic Operators, Union operators.. Asked in .....CTS....

    hint: can use Bitwise operators

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Is this an assignment? Have you written some code you need help with?

  3. #3
    krichait is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is this an assignment? Have you written some code you need help with?
    Thanks for response... this is not an assignment .. but asked in an interview... . I tried .. using bitwise operators...


    int add(int a, int b)
    {
    return add((a & b) << 1, a ^ b));
    }

    but actually it is not working...

  4. #4
    mtv134 is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    I can see ur using recursing
    where is the break condition?
    visit http://www.thejavacode.com
    Muhammad Safwat

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    Here's cod to do it bit by bit.

    Java Code:
    // 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
    }
    Last edited by Norm; 08-06-2008 at 08:11 PM.

  6. #6
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Wow norm...
    Good job...
    To finish sooner, take your own time....
    Nivedithaaaa

  7. #7
    krichait is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Wonderful.... NORM....

    Thanks vey much.............

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    I don't see how you could answer this question in a interview. My version took a couple of hours.

  9. #9
    mtv134 is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    Nice work Norm
    visit http://www.thejavacode.com
    Muhammad Safwat

  10. #10
    krichait is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Post Great Effort you have put in....

    Quote Originally Posted by Norm View Post
    I don't see how you could answer this question in a interview. My version took a couple of hours.
    It was asked to my friend.. in Cognizant Technology ...Company... they just give him to slove this problem in 45 minutes... but he couldn't... if he does the problem he got he job....

    Any Way.......... I got the code... but it is really unusefull to write such a code for simple addition....

    Thanks a lot..........

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 990
    Last Post: 07-24-2014, 03:28 AM
  2. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  3. Could some plz tell me how to write a stand alone
    By quickfingers in forum New To Java
    Replies: 25
    Last Post: 06-28-2008, 04:22 AM
  4. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM
  5. About bean:write
    By yuchuang in forum Web Frameworks
    Replies: 1
    Last Post: 04-30-2007, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •