班次操作员是如何实现的?

发布于 2024-09-25 14:08:06 字数 80 浏览 4 评论 0原文

我想知道当我执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人生戏 2024-10-02 14:08:07

通常这是一条处理器指令(直接在处理器上完成)。

它只是简单地移动内存中的位:

int a = 3; // a = 0 0 1 1
a << 1;    // a = 0 1 1 0 = 6
a << 1;    // a = 1 1 0 0 = 12

如果您正在寻找有关处理器如何在非常低的级别上工作的见解,代码查尔斯·佩措尔德 (Charles Petzold) 的《》是一本非常值得一读的书。

Normally this is a processor instruction (directly done on the processor).

It simply does shift the bits in memory:

int a = 3; // a = 0 0 1 1
a << 1;    // a = 0 1 1 0 = 6
a << 1;    // a = 1 1 0 0 = 12

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.

你的呼吸 2024-10-02 14:08:07

在您的特定情况下,编译器会将其快捷方式设置为常量,但一般来说,处理器的指令集包括特殊的操作码(操作码、命令)来执行位移操作。

这里很好地解释了其工作原理。

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.

赢得她心 2024-10-02 14:08:07

在硬件方面,每个输出位都是通过选择一个输入位来生成的。不需要乘法器,只需大量复用即可。

In hardware terms, each output bit is generated by selecting one of the input bits. No multipliers required, just a lot of multiplexing.

成熟的代价 2024-10-02 14:08:07

值得注意的是,有些处理器的指令可以在固定的时间内位移任意数量,有些处理器的指令可以移动可变的数量,但需要可变的时间来完成(因此左移 31 位可能需要一段时间),而其他指令仅限于一次移动一位的指令。虽然人们通常不必担心如此繁琐的实现细节,但实现共同结果的各种方法之间可能存在巨大的性能差异。

例如,使用(1<

  if (shift_amount & 16)
    longvar >>= 16;
  if (shift_amount & 8)
    longvar >>= 8;
  if (shift_amount & 4)
    longvar >>= 4; /* If processor has optimization for this */
  longvar >>= (shift_amount & 3);

如果存在用于快速移位的指令,则完全适得其反,但在某些 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<

  if (shift_amount & 16)
    longvar >>= 16;
  if (shift_amount & 8)
    longvar >>= 8;
  if (shift_amount & 4)
    longvar >>= 4; /* If processor has optimization for this */
  longvar >>= (shift_amount & 3);

Totally counter-productive if an instruction exists for fast shifting, but a potentially huge speedup (6x or better) on some 8-bit machines.

丿*梦醉红颜 2024-10-02 14:08:07

所有处理器可能都有移位指令,这里是其汇编语言的一些示例同行。

All processors probably have shift instructions, here are some examples of their assembly language counterparts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文