Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# difference between arithmetic operators and bitwise operators?

Minal Silimkar-Urankar
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 ?

Campbell Ritchie
Sheriff
Posts: 48954
60
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?

Garrett Rowe
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 ]

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15282
39
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
Sheriff
Posts: 48954
60
Originally posted by minal silimkar:
"How Arithmetic operators and Bitwise operators are different performancewise?". I can use >>1 instead of *2.