班次操作员是如何实现的?
我想知道当我执行 4<<2 时,下面到底发生了什么?是否执行任何乘法或如何计算该值。如果您对轮班操作员的实施有参考,请回复我。提前致谢
I want to know when I do 4<<2, what exactly happens underneath?? are there any multiplications performed or how is the value computed. if you have a reference to the implementation of shift operators please reply me. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常这是一条处理器指令(直接在处理器上完成)。
它只是简单地移动内存中的位:
如果您正在寻找有关处理器如何在非常低的级别上工作的见解,代码查尔斯·佩措尔德 (Charles Petzold) 的《》是一本非常值得一读的书。
Normally this is a processor instruction (directly done on the processor).
It simply does shift the bits in memory:
If you are looking for insight on how processors work in a very low level, Code by Charles Petzold is a fantastic book to read.
在您的特定情况下,编译器会将其快捷方式设置为常量,但一般来说,处理器的指令集包括特殊的操作码(操作码、命令)来执行位移操作。
这里很好地解释了其工作原理。
In your particular case the compiler will shortcut it to the constant, but in general instruction set of processors includes special opcodes (operation codes, commands) to perform bit shift operations.
Here's good explanation of how this works.
在硬件方面,每个输出位都是通过选择一个输入位来生成的。不需要乘法器,只需大量复用即可。
In hardware terms, each output bit is generated by selecting one of the input bits. No multipliers required, just a lot of multiplexing.
值得注意的是,有些处理器的指令可以在固定的时间内位移任意数量,有些处理器的指令可以移动可变的数量,但需要可变的时间来完成(因此左移 31 位可能需要一段时间),而其他指令仅限于一次移动一位的指令。虽然人们通常不必担心如此繁琐的实现细节,但实现共同结果的各种方法之间可能存在巨大的性能差异。
例如,使用(1<
如果存在用于快速移位的指令,则完全适得其反,但在某些 8 位机器上可能会带来巨大的加速(6 倍或更好)。
It's worthwhile to note that some processors have instructions that can bit shift any amount in a fixed amount of time, some have instructions which can shift a variable amount but will take a variable amount of time to do it (so shifting something left 31 bits may take awhile), and others are limited to instructions which shift one bit at a time. While one generally shouldn't worry about such fussy implementation details, there can be some huge performance differences between various ways of achieving common results.
For example, using (1<
Totally counter-productive if an instruction exists for fast shifting, but a potentially huge speedup (6x or better) on some 8-bit machines.
所有处理器可能都有移位指令,这里是其汇编语言的一些示例同行。
All processors probably have shift instructions, here are some examples of their assembly language counterparts.