1

I am trying to understand this for loop pattern:

                for {
                    // Shift n right by 1 before looping to halve it.
                    n := shr(1, n)
                } n {
                    // Shift n right by 1 each iteration to halve it.
                    n := shr(1, n)
                } { ... }

I guess it can be translated into

for(uint256 n = n/2; n > 0; n = n/2) { ... }

OR 

for(n >> 1; n > 0; n >> 1) { ... }

Is that correct ? I'm mainly concerned with the stop (middle) parameter but maybe even other parameters are incorrect.

1 Answer 1

0

This is solved. I'm pretty sure this is correct now as I can now get the desired result I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.