为什么Java和C#具有位移运算符? [英] Why do Java and C# have bitshifts operators?

查看:82
本文介绍了为什么Java和C#具有位移运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

注意:我测试了C#(使用gmcs Mono C#编译器版本2.6.7.0)的编译输出,并且即使乘以2的倍数,乘法示例也不使用shift进行乘法.

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.我忘记了在字节上使用它可能会有些有用,但是在将其用于数字时仍然遇到一些麻烦.

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.

推荐答案

是的,如果仅将移位运算符用作乘法的替代项,则应将其留给编译器.

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:

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

还有更多的事情需要位纠缠来实现,而无需使用本机代码.

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

这篇关于为什么Java和C#具有位移运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆