Home » Java » java – Why do i need to left shift before summing the result?-Exceptionshub

java – Why do i need to left shift before summing the result?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Here is a code snippet that works as intended, it correctly reverses the bits of an integer and returns the new int. My question is why does the line result <<= 1 have to occur before I sum result with the operation n&1?

In the first iteration, it does not affect results because the result is instantiated as 0. If I put the result <<= 1 line at the END of the for loop, I would still be left shifting result before I sum result with n&1.
My code, with the left shifting result at the end, isn’t working and I can’t seem to understand this final step.

public int reverseBits(int n) {
    int result = 0;
    for(int i=0; i<32; i++){
        result <<= 1;
        result += n&1;
        n >>= 1;
    }
    return result;
}
How to&Answers:

In the first iteration it indeed has no effect, but thats okay, thats intended. It is better, than writing if (i != 0) result <<= 1;. That would be inefficient to check this in every iteration. But also important to note this that essentially out of the 32 iteration only in 31 it will have an effect and should. So if you put the result <<= 1; line in the end than make sure it is only executing effectively 31 times also keeping in mind, that now it should execute in the first iteration since the order changed, but it should not in the last iteration. That is the reason why the result will be invalid.

Try this. This will work.

public int reverseBits(int n) {
    int result = 0;
    for(int i=0; i<32; i++){
        result += n&1;
        n >>= 1;
        if (i != 31) // skip the shift in last iteration
            result <<= 1;
    }
    return result;
}

Answer:

Because otherwise you operate on the wrong bit of your operation. Note you could replace your current bitshifts with simple arithmetic and division if that helps. Like,

public static int reverseBits(int n) {
    int result = 0;
    for (int i = 0; i < 32; i++) {
        result += result + (n & 1);
        n /= 2;
    }
    return result;
}

Explanation1:

result <<= 1;

This is equivalent to result = result * 2, which is the same as result = result + result thus we can combine with the next instruction;

result += n&1

and get

result += result + (n & 1);

As for >>= 1, that is the same as / 2.

1Math.

Answer:

This is a one-off problem:

  • you need to copy 32 bits, so you’re doing 32 result += n&1;operations (0..31)
  • to move the rightmost bit (position 0) to the leftmost position (position 31) you only need to do 31 left-shift operations.

Therefore, putting the result <<= 1; at the beginning is fine, as it actually does nothing, as there is nothing yet in result, so you’re doing only 31 left-shifts.
Only when you move it after the result += n&1; you’re doing 32 left-shift operations, loosing one bit.
The same holds for the n >>= 1;, where you do 32 right-shift operations as well, but the final right-shift doesn’t matter, as it is never added to the result (and zero anyway).