为什么 Java 和 C# 有位移运算符?

发布于 2024-09-25 08:00:41 字数 473 浏览 10 评论 0原文

整数乘法(暂时忘记除法)之间的差异是否仍然有利于移位,如果是的话,差异有多大?

它看起来只是一个低级优化,即使您想要它,在大多数情况下(C#/Java)字节码编译器或 jit 不应该捕获它吗?

注意:我测试了 C# 的编译输出(使用 gmcs Mono C# 编译器版本 2.6.7.0),即使乘以 2 的倍数,乘法示例也没有使用移位进行乘法。C

# http://csharp.pastebin.com/hcrRnPrb

cil http://csharp.pastebin.com/0js9F2c1

PS 我忘记了在字节上使用它可能会有一些用处,但在将它用于数字时仍然遇到一些麻烦。

Is the difference between integer multiply(temporarily forgetting about division) still in favor of shifting and if so how big is the difference?

It simply seems such a low level optimization, even if you wanted it the shouldn't the (C#/Java) to bytecode compiler or the jit catch it in most cases?

Note: I tested the compiled output for C#(with gmcs Mono C# compiler version 2.6.7.0) and the multiply examples didn't use shift for multiplying even when multiplying by a multiple of 2.

C#
http://csharp.pastebin.com/hcrRnPrb

cil
http://csharp.pastebin.com/0js9F2c1

P.S.
I forgot how it might be somewhat useful to use it on bytes, but still having some trouble on using it for Numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

一曲琵琶半遮面シ 2024-10-02 08:00:41

第一个原因:

有时 - 大多数时候 - 您希望将整数视为数字。有时,整数是表示一组位的便捷方法。

乘法是对数字的运算。

移位是对一组位的操作。

乘法结果和移位结果之间恰好存在关系并不是特别相关。这些操作在逻辑上是不同的。

第二个原因:

C# 和 Java 的设计目的都是为了让 C 开发人员熟悉,尽管只是肤浅的了解。因此,C 中的常见习惯用法被包含在 C# 和 Java 中。

First reason:

Sometimes - most times - you want to treat an integer as a number. Sometimes though an integer is a convenient way to represent a set of bits.

Multiplication is an operation on numbers.

Shifting is an operation on a set of bits.

That there happens to be a relationship between the results of multiplication and the results of shifting is not particularly relevant. The operations are logically different.

Second reason:

C# and Java were both designed to be familiar to C developers, albeit at a superficial level. Therefore common idioms from C were included in C# and Java.

痴情换悲伤 2024-10-02 08:00:41

如果我想将一个数字乘以 4,我会写 * 4。如果我的目的是将某些位左移 2 个位置,我会写 << 2.

回复问题:

为什么 Java 和 C# 有位移运算符?

我在二进制数据上做了很多工作,其中我不考虑整数等 - 只是二进制 - 在该领域不断使用移位运算符完全合乎逻辑

当然,我可以输入* 2等,但是我真正想做的是移位位。

这在字节很重要的一系列领域(例如图形编程、序列化等)中很常见。

此外,移位操作有一些微妙,您希望它表现得像一个整数,特别是在处理边缘时......当您向左移动一点离开地图,或者向右移动位进入地图(-ve 与 +ve 等)时,就会发生这种情况,这很好理解,但很关键。同样,整数乘法的检查/未检查行为有时非常重要。

If I wanted to multiply a number by 4, I would write * 4. If my intent is to left-shift some bits 2 places, I would write << 2.

Re the question:

Why do Java and C# have bitshifts operators?

I do a lot of work on binary data, where I'm not thinking about integers etc - just binary - and in that area it is entirely logical to use shift operators constantly.

Sure, I could type * 2 etc, but what I actually want to do is shift the bits.

This is common in a range of areas where bytes matter (for example graphics programming, serialization, etc).

Additionally, there are some subtleties of shift operations where you don't want it to behave like an integer, in particular when dealing with the edges... the rules for what happens when you left-shift a bit off the map, or right-shift bits into the map (-ve vs +ve etc) are well understood but critical. Likewise, the checked/unckecked behaviour of integer multiplication is sometimes very important.

南汐寒笙箫 2024-10-02 08:00:41

你是对的,如果移位运算符仅用作乘法的替代,那么它应该留给编译器。

我想您忽略了以下应用程序:

  • 加密/解密
  • CRC 计算
  • 位图操作(图形、数据库锁)
  • 压缩/解压缩
  • 为硬件寄存器设置数据
  • 更改编码

等等,需要位旋转才能在没有本机代码的情况下有效实现。

You are right, if shift operators are used only as an alternative for multiplications, it should be left to the compiler.

I suppose you overlooked applications like:

  • Encryption / decryption
  • CRC calculation
  • Bitmap manipulation (Graphics, Database locks)
  • Compression/Decompression
  • Setting up data for hardware registers
  • Change encoding

and much more need bit-twiddling for efficient implementation without native code.

静水深流 2024-10-02 08:00:41

您问的本质上不是为什么 C#/Java 中有位移运算符,而是为什么 javac 编译器不将 2 的幂乘法和除法优化为位移。

对此的本能反应是乘法和除法与位移位具有不同的语义,因此它不能 100% 映射来替换操作。

此外,您还忘记了 JIT(热点)中发生的额外编译步骤,其中发生了各种额外的优化。坦率地说,没有必要优化这个特定的步骤,这与 C 不同,C 中的代码是编译器生成的。

What you are asking is essentially not why there are bitshift operators in C#/Java, but why the javac compiler doesn't optimize multiplications and divisions with powers of two into bitshifts.

The knee-jerk reaction to this is that multiplication and division have different semantics than bitshifts, so it does not map 100% to replace the operations.

Also, you forgot the extra compilation step that happens in the JIT (HotSpot) where all kinds of additional optimizations happen. There is frankly no need to optimize this particular step, as opposed to C where the code is as the compiler generates it.

荒人说梦 2024-10-02 08:00:41

因为语言设计者认为拥有它们会很好。

它们是否等同于其他操作以及编译器是否足够智能以有效地实现这些操作并不重要。如果这就是我们的目标,那么您只需要一个宏汇编器和一个非常好的链接时间优化器,也许在带有垃圾收集器的虚拟机上。这些不是语言设计者通常追求的目标。

Because the language designers thought it would be good to have them.

It's not really important that they are equivalent to some other opperation, and that the compilers are smart enough to implement the operations efficiently. If that's where we're going, then you don't need much more than a macro assembler and a really good link time optimizer, maybe on a VM with a garbage collector. Those are not the goals language designers normally pursue.

葬﹪忆之殇 2024-10-02 08:00:41

例如,您的程序可能使用位掩码之类的东西。在这种情况下,位移操作是必要的。或者,如果您只是解决一些奇怪的任务,需要以指定的方式对状态进行编码。

观看这个教程 - 大部分示例来自数学问题。如果您只是制作一个网站或 GUI 应用程序,您可能不需要转移,但有时您确实需要......

For example, your program may use something like bit masks. In that case bitshift operation is a necessity. Or if you are just solving some weird task that requires encoding of states in a specified manner.

Watch this tutorial - most of the samples come from math problems. If you are simply making a site or a GUI application, you probably do not need shifting, but sometimes you really do...

唠甜嗑 2024-10-02 08:00:41

除了此处的其他原因之外,在很多情况下,您可能需要进行移位(或其他位操作)以通过网络与第三方库或远程应用程序进行交互。

In addition to the other reasons here, there are plenty of cases where you may need to shift (or other bit operations) to interface with a 3rd party library or a remote application over a network.

柳若烟 2024-10-02 08:00:41

这样你就可以左右移动位。您想要这些位及其移位操作代表什么完全取决于您。

So that you can shift bits left and right. What you want those bits and their shift operations to represent is entirely upto you.

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