Home > Java > javaTutorial > body text

How to use bitwise operations in java instead of multiplication and division

WBOY
Release: 2023-05-16 20:31:04
forward
713 people have browsed it

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;
Copy after login

Optimization for multiplication and division operations of integers

a*=2  a/=2
Copy after login

Using bit operations can be written as

a<<=1a>>=1
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template