Why do Java and C# have bitshifts operators?

南笙酒味 提交于 2021-02-16 16:59:49

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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...




回答7:


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.




回答8:


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



来源:https://stackoverflow.com/questions/3836641/why-do-java-and-c-sharp-have-bitshifts-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!