Cut and dry... while I never have enough logical operations for it to be a performance bottleneck - I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to the same-named logical operators (&& and ||) if possible? Perhaps the question can be prefaced by the fact that I don't know of a library to convert Java to assembly to see the # of operations.
-
Take a look at this answer: http://stackoverflow.com/a/7415759/207868 It might be what you want to know. – Poindexter Jun 15 '12 at 14:15
8 Answers
Bitwise operators avoid branching instructions, even in Java code execution. As a result you have no expensive branch prediction misses and no jumps at all.
From my experience, they can be measurably faster when used in code that is executed often enough. Keep in mind, though, that the bitwise operators are not short-circuiting ones, which may actually have a negative impact on performance in some cases.
That said, such micro-optimizations should only be used as a last resort and only after a profiler tells you to do so - readability and maintainability comes first.

- 84,049
- 23
- 157
- 201
-
5+1 for calling out the functional difference (bitwise ops don't short-circuit)! – Sbodd Jun 15 '12 at 14:23
Most of that would just be optimized by the compiler anyway. A quick Google shows this handy guide to viewing your Java as assembler. I've always been of the opinion that legible, human-readable code is more important than a few fewer nanoseconds of CPU time.
Java isn't the greatest language to pull extreme speed from thanks to the extra layer of the JVM. If you're interested in such precise optimizations, you may want to move to another language such as C/C++. This list shows what languages you may want to look into.

- 2,547
- 17
- 16

- 38,868
- 19
- 114
- 143
-
2Why would the extra layer containing a highly efficient and aggressive compiler mean that extreme speed is not possible? – Thorbjørn Ravn Andersen Jun 15 '12 at 14:19
-
It's another layer of abstraction. Java loses some speed, but is cross-platform. In most cases, the speed loss is negligible, but if the asker is worried about get a program to run that fast, they may want to check out this list: http://shootout.alioth.debian.org/ – SomeKittens Jun 15 '12 at 14:21
I suggest you watch Josh Bloch's "Performance Anxiety" talk on Parleys.com. http://www.parleys.com/#st=5&id=2103&sl=1
**update ** sadly, after some years the link is not valid anymore

- 4,217
- 1
- 34
- 39
-
Hey there, I'm interested in learning more about this talk. This link seems to no longer work, do you happen to have another link that shows the same talk? – FireController1847 Sep 22 '20 at 02:34
I wonder, would I be better off using bitwise and (&) and bitwise or (|) as opposed to logical operators if possible?
Strange, you went from asking a trivia question about performance to asking if you should actually do it in your code. Well the second one is easy. No. The costs to you as a developer writing less legible code will overwhelm the nanosecond difference in CPU cost. If you need to optimize this much anyway use C or C++.

- 59,258
- 35
- 162
- 290
-
4*"...overwhelm the nanosecond difference in CPU cost"* If even that, +1. "*If you need to optimize this much anyway use C or C++."* -1 ;-) HotSpot is a freakin' good runtime optimizer. – T.J. Crowder Jun 15 '12 at 14:17
-
2As someone who's optimized code this deeply, yes, it will make a measurable difference in nanoseconds. Not in tens of nanoseconds, though. – Louis Wasserman Jun 15 '12 at 15:18
The Java compiler just compiles to byte code which is rather far from actual machine code. It is the responsibility of the JVM to do that, and modern JVM's like HotSpot are quite good at doing so. Hence write the simplest, clearest code that does what you need to do.
In short, you will most likely not be able to measure any difference.
To see the actual machine code generated you need to ask the JVM to show you. This is vendor dependent.

- 73,784
- 33
- 194
- 347
you could try writing a small program with say 100000 bitwise ops, use the timer function to determine the runtime. Then do the same thing for the logical ops. Run them a few times and check out the results.

- 1,330
- 12
- 21
No.
First, using bitwise operators in contrast to logical operators is prone to error (e.g., doing a right-shift by 1 is NOT equivalent to multiplying by two). Second, the performance benefit is negligible (if any).
Last but not least, using logical operators conveys meaning much better.

- 59,493
- 71
- 188
- 276
Like schrodinger's cat Yes and No at the same time.
It depends on what you're doing really ! I once did a sudoku solver with and without bitwise operation. Here my benchmark :
- With : 0.9 millis
- Without : 50 millis
I was using backtracking algorithm so it explain a lot why it was so much faster with bitwise operation since sodoku is a NP-Complete (maybe NP-Hard) problem.
But, like other already told you, it is really difficult to read and maintain (I'll never go back in my sudoku solver to make any changes, I wouldn't understand at some place what I did).
In general, bitwise operation are always faster then any counterpart but unless what you're doing is the bottle neck of an critical software, I wouldn't recommends using it for no reason other than that one.

- 756
- 6
- 17