Why do i need to left shift before summing the result?

RagnarRock8

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;
}
Eraklon

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do i need to left shift before summing the result?

From Dev

Why do I need to cast a Float to float before cast to byte?

From Dev

Why do I need a delay before showing AlertDialog?

From Dev

Gstreamer: Why do I need a videoconvert before displaying some filter?

From Dev

Why do I need to type `./` before executing a program in the current directory?

From Dev

Why do I need to type `./` before executing a program in the current directory?

From Dev

Why do I need to initialise this variable before the user assigns a value to it?

From Dev

PHP When and why do I need to call store_result()?

From Dev

PHP When and why do I need to call store_result()?

From Dev

Why do I need to declare encoding before hashing in Python, and how can I do this?

From Dev

Why does left shift and right shift in the same statement yields a different result?

From Dev

Why do I need to change the Binding Source Position before I can SaveChanges

From Dev

Why do I need the double dash before argument passed to sh by xargs -I for it to work properly?

From Dev

MySQL check if value is in result set before summing

From Dev

why i need clear stringstream before reuse?

From Dev

Why do I need a FactorySupplier?

From Dev

Why do I need dbus?

From Dev

why do I need 2>&1 before grep when using a pipe

From Dev

Why do I need to run find_rtools() before has_devel() = TRUE?

From Dev

Why do i need to add final keyword before int empAge in 4th line?

From Dev

Why do I need to renew my IP address every time I start my computer before I can get internet access?

From Dev

Do I need to need to deactivate Windows before re-installing

From Dev

Need some help - left shift bitwise operator

From Dev

Need some help - left shift bitwise operator

From Dev

Weird result of Java Integer left shift

From Java

Do I need to explicitly handle negative numbers or zero when summing squared digits?

From Dev

Typecasting int to float before division. Which casts do I really need and which can I remove and why?

From Dev

When do I need to pull before I can push in Git?

From Dev

When do I need to pull before I can push in Git?

Related Related

  1. 1

    Why do i need to left shift before summing the result?

  2. 2

    Why do I need to cast a Float to float before cast to byte?

  3. 3

    Why do I need a delay before showing AlertDialog?

  4. 4

    Gstreamer: Why do I need a videoconvert before displaying some filter?

  5. 5

    Why do I need to type `./` before executing a program in the current directory?

  6. 6

    Why do I need to type `./` before executing a program in the current directory?

  7. 7

    Why do I need to initialise this variable before the user assigns a value to it?

  8. 8

    PHP When and why do I need to call store_result()?

  9. 9

    PHP When and why do I need to call store_result()?

  10. 10

    Why do I need to declare encoding before hashing in Python, and how can I do this?

  11. 11

    Why does left shift and right shift in the same statement yields a different result?

  12. 12

    Why do I need to change the Binding Source Position before I can SaveChanges

  13. 13

    Why do I need the double dash before argument passed to sh by xargs -I for it to work properly?

  14. 14

    MySQL check if value is in result set before summing

  15. 15

    why i need clear stringstream before reuse?

  16. 16

    Why do I need a FactorySupplier?

  17. 17

    Why do I need dbus?

  18. 18

    why do I need 2>&1 before grep when using a pipe

  19. 19

    Why do I need to run find_rtools() before has_devel() = TRUE?

  20. 20

    Why do i need to add final keyword before int empAge in 4th line?

  21. 21

    Why do I need to renew my IP address every time I start my computer before I can get internet access?

  22. 22

    Do I need to need to deactivate Windows before re-installing

  23. 23

    Need some help - left shift bitwise operator

  24. 24

    Need some help - left shift bitwise operator

  25. 25

    Weird result of Java Integer left shift

  26. 26

    Do I need to explicitly handle negative numbers or zero when summing squared digits?

  27. 27

    Typecasting int to float before division. Which casts do I really need and which can I remove and why?

  28. 28

    When do I need to pull before I can push in Git?

  29. 29

    When do I need to pull before I can push in Git?

HotTag

Archive