Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between arithmetic operators and bitwise operators?

 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 15366
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by minal silimkar:
"How Arithmetic operators and Bitwise operators are different performancewise?". I can use >>1 instead of *2.
Not what you asked earlier.

I agree with Jesper and Garrett. And you have shown how using strange operators can lead to errors; you have written >> when you meant <<!
[ July 09, 2008: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic