Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3931258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:20:05+00:00 2026-05-19T23:20:05+00:00

I have the methods that do both the multiplication and addition, but I’m just

  • 0

I have the methods that do both the multiplication and addition, but I’m just not able to get my head around them. Both of them are from external websites and not my own:

public static void bitwiseMultiply(int n1, int n2) {
    int a = n1, b = n2, result=0;
    while (b != 0) // Iterate the loop till b==0
    {
        if ((b & 01) != 0) // Logical ANDing of the value of b with 01
        {
            result = result + a; // Update the result with the new value of a.
        }
        a <<= 1;              // Left shifting the value contained in 'a' by 1.
        b >>= 1;             // Right shifting the value contained in 'b' by 1.
    }
    System.out.println(result);
}

public static void bitwiseAdd(int n1, int n2) {
    int x = n1, y = n2;
    int xor, and, temp;
    and = x & y;
    xor = x ^ y;

    while (and != 0) {
        and <<= 1;
        temp = xor ^ and;
        and &= xor;
        xor = temp;
    }
    System.out.println(xor);
}

I tried doing a step-by-step debug, but it really didn’t make much sense to me, though it works.

What I’m possibly looking for is to try and understand how this works (the mathematical basis perhaps?).

Edit: This is not homework, I’m just trying to learn bitwise operations in Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:20:06+00:00Added an answer on May 19, 2026 at 11:20 pm

    Let’s begin by looking the multiplication code. The idea is actually pretty clever. Suppose that you have n1 and n2 written in binary. Then you can think of n1 as a sum of powers of two: n2 = c30 230 + c29 229 + … + c1 21 + c0 20, where each ci is either 0 or 1. Then you can think of the product n1 n2 as

    n1 n2 =

    n1 (c30 230 + c29 229 + … + c1 21 + c0 20) =

    n1 c30 230 + n1 c29 229 + … + n1 c1 21 + n1 c0 20

    This is a bit dense, but the idea is that the product of the two numbers is given by the first number multiplied by the powers of two making up the second number, times the value of the binary digits of the second number.

    The question now is whether we can compute the terms of this sum without doing any actual multiplications. In order to do so, we’re going to need to be able to read the binary digits of n2. Fortunately, we can do this using shifts. In particular, suppose we start off with n2 and then just look at the last bit. That’s c0. If we then shift the value down one position, then the last bit is c0, etc. More generally, after shifting the value of n2 down by i positions, the lowest bit will be ci. To read the very last bit, we can just bitwise AND the value with the number 1. This has a binary representation that’s zero everywhere except the last digit. Since 0 AND n = 0 for any n, this clears all the topmost bits. Moreover, since 0 AND 1 = 0 and 1 AND 1 = 1, this operation preserves the last bit of the number.

    Okay – we now know that we can read the values of ci; so what? Well, the good news is that we also can compute the values of the series n1 2i in a similar fashion. In particular, consider the sequence of values n1 << 0, n1 << 1, etc. Any time you do a left bit-shift, it’s equivalent to multiplying by a power of two. This means that we now have all the components we need to compute the above sum. Here’s your original source code, commented with what’s going on:

    public static void bitwiseMultiply(int n1, int n2) {
        /* This value will hold n1 * 2^i for varying values of i.  It will
         * start off holding n1 * 2^0 = n1, and after each iteration will 
         * be updated to hold the next term in the sequence.
         */
        int a = n1;
    
        /* This value will be used to read the individual bits out of n2.
         * We'll use the shifting trick to read the bits and will maintain
         * the invariant that after i iterations, b is equal to n2 >> i.
         */
        int b = n2;
    
        /* This value will hold the sum of the terms so far. */
        int result = 0;
    
        /* Continuously loop over more and more bits of n2 until we've
         * consumed the last of them.  Since after i iterations of the
         * loop b = n2 >> i, this only reaches zero once we've used up
         * all the bits of the original value of n2.
         */
        while (b != 0)
        {
            /* Using the bitwise AND trick, determine whether the ith 
             * bit of b is a zero or one.  If it's a zero, then the
             * current term in our sum is zero and we don't do anything.
             * Otherwise, then we should add n1 * 2^i.
             */
            if ((b & 1) != 0)
            {
                /* Recall that a = n1 * 2^i at this point, so we're adding
                 * in the next term in the sum.
                 */
                result = result + a;
            }
    
            /* To maintain that a = n1 * 2^i after i iterations, scale it
             * by a factor of two by left shifting one position.
             */
            a <<= 1;
    
            /* To maintain that b = n2 >> i after i iterations, shift it
             * one spot over.
             */
            b >>>= 1;
        }
    
        System.out.println(result);
    }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm debugging, I often have to deal with methods that does not use
I have two methods that I need to run, lets call them metA and
Some existing web services I consume have methods that look something like this: List<Employee>
I'm creating my own JavaScript Array-like object and I have methods that call closures.
We have some methods that call File.Copy, File.Delete, File.Exists, etc. How can we test
I have some WCF methods that are used to transmit information from a server
I have methods with more than one parameter that are guarded against bad input
Ok so I have a number of methods that look like this:- which sorts
I have some ASP.NET page and webservice WebMethod() methods that I'd like to add
I have several service classes that have static methods and offer a service to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.