Bitwise operations replace multiplication and division
Among all operations, bitwise operations are the most efficient. Therefore, you can try to use bit operations to replace some arithmetic operations to improve the running speed of the system.
For example, bit operations are used in the source code of HashMap
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 static final int MAXIMUM_CAPACITY = 1 << 30;
Optimization for multiplication and division operations of integers
a*=2 a/=2
Using bit operations can be written as
a<<=1a>>=1
The above is the detailed content of How to use bitwise operations in java instead of multiplication and division. For more information, please follow other related articles on the PHP Chinese website!