简体   繁体   中英

what does the following C code translate to?

So I have a for loop which goes like this:

for(span=N>>1;span;span>>=1)

I am assuming the start and end conditions are equivalent to as follows:

span = N>>1; //right shift N by 1 and initialize to span
while(span!=0)
{
 span = span >> 1;
}

However it seems a little bizarre in the context of my code. Thanks in advance!

In every iteration you are dividing the variable span by 2 until it reaches 0.

so if initially N = 8, then values for span will be 4, 2, 1, 0 -> exit loop

That is correct.

  • The initializer sets span = N >> 1 , right-shifting N by 1.
  • The loop condition is span , which is equivalent to span != 0 .
  • Every time the loop comes around, span is right-shifted again by 1.

In the context of positive integers, this is equivalent to for(span=N/2;span>0;span/=2) . However, without knowing your particular context I cannot comment whether this is or is not bizarre.

简短而甜美...是的,只要您已经展示了代码,您的解释就坚定而良好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM