aspose file tools*
The moose likes Java in General and the fly likes difference between arithmetic operators and bitwise operators? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "difference between arithmetic operators and bitwise operators?" Watch "difference between arithmetic operators and bitwise operators?" New topic
Author

difference between arithmetic operators and bitwise operators?

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
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 ?


Minal Silimkar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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
Ranch Hand

Joined: Nov 25, 2007
Posts: 133

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

Joined: Jan 17, 2006
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between arithmetic operators and bitwise operators?
 
Similar Threads
Bitwise and Bit Shift on exam?
Logical vs bitwise operators
about operator
logical operators..
Increment and Decrement Operators