简体   繁体   English

以下C代码转换为什么?

[英]what does the following C code translate to?

So I have a for loop which goes like this: 所以我有一个for循环,如下所示:

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. 在每次迭代中,您都将变量跨度除以2,直到达到0。

so if initially N = 8, then values for span will be 4, 2, 1, 0 -> exit loop 因此,如果最初N = 8,则span的值将为4,2,1,0->退出循环

That is correct. 那是对的。

  • The initializer sets span = N >> 1 , right-shifting N by 1. 初始化程序设置span = N >> 1 ,将N右移1。
  • The loop condition is span , which is equivalent to span != 0 . 循环条件是span ,它等于span != 0
  • Every time the loop comes around, span is right-shifted again by 1. 每次循环出现时, span再次右移1。

In the context of positive integers, this is equivalent to for(span=N/2;span>0;span/=2) . 在正整数的上下文中,这等效于for(span=N/2;span>0;span/=2) However, without knowing your particular context I cannot comment whether this is or is not bizarre. 但是,在不知道您的特定上下文的情况下,我无法评论这是不是奇怪。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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