Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

difference between arithmetic operators and bitwise operators?

Ranch Hand
Posts: 136
What is the real difference between arithmetic operators and bitwise operators?

One I know bitwise opearators are faster than arithmetic operators.

What are the other differences ?

Marshal
Posts: 69806
277
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.

Minal Silimkar-Urankar
Ranch Hand
Posts: 136

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?

Ranch Hand
Posts: 1296
What do you mean how are they different? The obvious answer is that they're defined differently and do different things. They take different operands to achieve the same result.

5 * 2.0 = 10.0
5 / 0.5 = 10.0
5 + 5.0 = 10.0
5 - (-5.0) = 10.0

They're different in the same way that all the above operators are different.

Minal Silimkar-Urankar
Ranch Hand
Posts: 136

Originnaly posted by Garrett Rowe

What do you mean how are they different? The obvious answer is that they're defined differently and do different things. They take different operands to achieve the same result.

5 * 2.0 = 10.0
5 / 0.5 = 10.0
5 + 5.0 = 10.0
5 - (-5.0) = 10.0

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 ]

Java Cowboy
Posts: 16084
88
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.

Campbell Ritchie
Marshal
Posts: 69806
277

Originally posted by minal silimkar:
"How Arithmetic operators and Bitwise operators are different performancewise?". I can use >>1 instead of *2.