Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 287
Roman A. Taycher Avatar asked Oct 01 '10 04:10

Roman A. Taycher


People also ask

Why do we use C in Java?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

What is difference between Java and C?

KEY DIFFERENCES: C is a Procedural Programming Language whereas Java is an Object-Oriented language. C is middle level language while Java is high level language. C does not support threading on the other hand Java has a feature of threading. C supports pointers but Java does not support pointers.

Should I learn both C and Java?

Yes, you can learn both of them at the same time. But, it's advisable to Learn C first and then Java. C is a Structural Programming Language and a middle level language. It forms the basis for Computers and embedded systems.

Can you use Java and C together?

If you want to call native code from Java program, you should use JNI. This will require preparation in the C side of your code, but it works pretty good. If you are new to programming, again, I would recommend avoiding that. Regarding GUI - you can work with Swing.


4 Answers

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.

like image 72
Eric Lippert Avatar answered Oct 05 '22 11:10

Eric Lippert


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.

like image 41
Marc Gravell Avatar answered Oct 05 '22 12:10

Marc Gravell


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.

like image 25
stacker Avatar answered Oct 05 '22 10:10

stacker


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.

like image 45
Thorbjørn Ravn Andersen Avatar answered Oct 05 '22 10:10

Thorbjørn Ravn Andersen