Should I use bitwise operators in methods?

I am very new to Java (and generally programming, my previous experience is with ActionScript 2.0 and some simple JavaScript), and I am slowly and methodically working in Java: Herbert Schildt's Beginner's Guide. This is an incredible book.

Firstly, I finally understand more or less what bitwise operators (which I first encountered in ActionScript 2.0), and that they are more efficient than other methods for certain sums.

My question is: is it more efficient to use a method that uses, say, shift right to do all your divisions / 2 (or divisions / evens) for you in a large program with many calculations (in this case, an expanding RPG), or is it more efficient just use standard math operations because the compiler optimizes all this for you?

Or am I asking the wrong question entirely?

+4
source share
6 answers

You are asking the wrong question. The question you should ask is "should I keep my code simple and readable or use tricks that I believe will improve its performance, although I did not appreciate its performance." The answer should be obvious.

Bitwise operators have their place, especially when dealing with binary file formats that need to pack a large amount of data into a small space. And it’s absolutely important to know how to mask the most significant bits of a byte so that you don’t accidentally subscribe (print these two variables to understand what I mean):

byte b = (byte)0xDC; int i = b & 0xFF; 

But do not look for places to use these operators, especially to replace such simple tasks as dividing by 2.

+9
source

Optimizations like this should only be done if you have a real need for it. If you just think that the code will run faster, it might not be worth it.

Often the compiler can be smarter than you think and do the optimization for you, in other cases there may be caveats that only exacerbate the problem. At the top of this (and probably the biggest reason for this), is that if it makes your code read / understand for future developers (or yourself), you can add additional anti-optimizations in the future, trying to work around the source code.

+4
source

Usually the compiler does a lot of optimizations. You also need to find out what your bottleneck is before you optimize it. Optimizing things that are not a bottleneck just means that your threads get into a real bottleneck faster. Check out the theory of constraints.

+4
source

For training purposes, you can skip code optimization this way. This requires some experience, otherwise you may encounter problems debugging your code (basically I mean "what is this thing?"). Compilers are now smart enough to optimize the output code and correctly deal with well-known patterns. If you really don't need to save every millisecond or processor cycle, focus on keeping your code clean.

+3
source

Last. In other words, first write your code in the clearest way, which is likely to entail standard mathematical operations. Let the compiler take care of the rest.

Java, since it runs on a virtual machine, has other interesting built-in optimization functions: for example, as your program starts, the VM can see which code branches are executed most often and make these branches more efficient.

Once you write your program, use the profiling tool to measure which specific methods are slow. Once you know this, you will know which code needs to be optimized. For example, you may find that reading the configuration from a file is the slowest thing you do, and you can look for ways to make it faster. My guess is that you are unlikely to find any performance advantage using things like bitwise operators rather than regular arithmetic.

Good luck

+2
source

First, I finally understand more or less that operators (which I first encountered in ActionScript 2.0) are beaten, and that they are more efficient than other methods for certain amounts.

There is nothing wrong with that. Bitwise operations have more applications that do the division or multiplication you are asking for.
You can effectively use bitmasks as flags, package values, emulate access rights in a compact form, in crytography, etc. Etc.
Watch this stream. You should look for more applications than just reading the chapter that talks about how the shift is similar and (possibly) faster than division (not necessary, since the compiler will convert anyway)

0
source

All Articles