Arithmetic operators do arithmetic and bitwise operators do bit manipulation. There are some things like * 2 which are quicker using << 1, but in most cases the two are completely different and independent. You can read a little about bitwise operators in the Java™ Tutorials.
Joined: Nov 25, 2007
Originally posted by Campbell Ritchie Arithmetic operators do arithmetic and bitwise operators do bit manipulation. There are some things like * 2 which are quicker using << 1, but in most cases the two are completely different and independent. You can read a little about bitwise operators in the Java� Tutorials.
Output of above code is same, i.e. 10. How both operators are different?
They're different in the same way that all the above operators are different.
My qestion was "How arithmetic operators are differnt than Bitwise operators?". I know *, /, +, - this are performing different operations as they're all arithmetic operators. I would like to know, "How Arithmetic operators and Bitwise operators are different performancewise?". I can use >>1 instead of *2. Output for both will be same. But when to use arithmetic operators and when to use bitwise operators? [ July 09, 2008: Message edited by: minal silimkar ]
You should always use the most obvious operator for whatever calculation you need to do.
Using << instead of * because it's faster only helps a tiny bit for performance, but it makes your code less clear. Most of the time, the small performance gain is not worth the cost of making your code more difficult to understand.
If you need to improve performance, do it by using a profiler to measure where the performance bottlenecks are in your application, and try to improve those. Micro-optimizations like using << instead of * are almost never useful.
So, just use * 2 when you need to multiply a number by 2.