Python 3 - 什么是“>”
这是令人困惑的一行: x_next = (x_next + (a // x_prev)) >> 1
This is the confusing line: x_next = (x_next + (a // x_prev)) >> 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这是令人困惑的一行: x_next = (x_next + (a // x_prev)) >> 1
This is the confusing line: x_next = (x_next + (a // x_prev)) >> 1
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
它是逐位移位。接下来会给你一些直观的感受:
It is bit-wise shift. The next will give you some intuitions:
>>
运算符与 C 和许多其他语言中的运算符相同。向右移动一位。如果您的数字是这样的二进制:
0100
,那么在>>之后它将是
.与0010
1.>> 2
它将是0001
。所以基本上这是一个将你的数字除以 2 的好方法(同时保留余数);)
The
>>
operator is the same operator as it is in C and many other languages.A bitshift to the right. If your number is like this in binary:
0100
than it will be0010
after>> 1
. With>> 2
it will be0001
.So basically it's a nice way to divide your number by 2 (while flooring the remainder) ;)
它是右移运算符。
这里它被用来除以 2。将其写为会更清楚。
可悲的是,很多人试图变得聪明并使用移位运算符来代替乘法和除法。通常,这只会给那些稍后必须阅读代码的穷人带来很多困惑。
It is the right shift operator.
Here it is being used to divide by 2. It would be far more clear to write this as
Sadly a lot of people try to be clever and use shift operators in place of multiplication and division. Typically this just leads to lots of confusion for the poor individuals who have to read the code at a later date.
大多数新/年轻的程序员并不担心效率,因为计算机速度非常快。
但是,如果您使用的是 8 位或 16 位处理器,可能有也可能没有硬件乘法,并且很少有硬件除法,那么移位整数需要一个机器周期,而乘法可能需要 16 或更多,除法可能需要 16 个机器周期。需要 50-200 个机器周期。当您的处理器时钟在 GHz 范围内时,您不会注意到差异,但如果您的指令速率为 8 MHz 或更低,则加起来会非常快。
因此,为了提高效率,人们转向使用 2 的幂进行乘法或除法,特别是在小型处理器和控制器最常用的 C 语言中。
我经常看到它,以至于我什至不再考虑它。
我在 C 中做的一些事情:
x = y >> 3; // 与除以 8 的下限相同
if (y & 0x04) x++; // 添加舍入,以便对结果进行舍入
对于大多数微控制器,编译器可让您查看生成的结果机器代码,并且您可以查看生成的不同语句。有了这种类型的反馈,过了一段时间你就开始编写更高效的代码。
Most newer/younger programmers do not worry about efficiency because the computers are so fast.
But if you are working on a 8-bit or 16-bit processor that may or may not have a hardware multiply and rarely has a hardware divide, then shifting integers takes one machine cycle while a multiply may take 16 or more and a divide may take 50-200 machine cycles. When your processor clock is in the GHz range you do not notice the difference, but if your instruction rate is 8 MHz or less it adds up very quickly.
So for efficiency people shift to multiply or divide for powers of two, especially in C which is the most common language for small processors and controllers.
I see it so often that I do not even think about it anymore.
Some of the things I do in C:
x = y >> 3; // is the same ad the floor of a divide by 8
if (y & 0x04) x++; // adds the rounding so the answer is rounded
For most microcontrollers, the compiler lets you see the resulting machine code generated and you can see what different statements generate. With that type of feedback, after awhile you just start writing more efficient code.
意思是“右移”。它的工作原理与按
2
划分楼层相同:It means "right shift". It works the same as floor division by
2
: