Search code examples
c#javalow-levelpremature-optimizationbit-shift

Why do Java and C# have bitshifts operators?


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.


Solution

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