Fundamentals 10 min read

Five Classic Bitwise Operation Techniques in Java

This article introduces five classic bitwise operation examples in Java—including checking odd/even, swapping variables without a temporary, finding a unique number, fast exponentiation, and determining the greatest power of two not exceeding N—explaining the underlying binary logic, code implementations, and their efficiency advantages.

Java Captain
Java Captain
Java Captain
Five Classic Bitwise Operation Techniques in Java

This article demonstrates the speed and elegance of bitwise operations through five classic examples written in Java.

1. Determine Odd or Even

Instead of using the modulo operator, the least‑significant bit can be checked directly.

if( n % 2) == 01
// n 是个奇数
}

Using bitwise AND yields the same result more efficiently:

if(n & 1 == 1){
// n 是个奇数。
}

2. Swap Two Numbers Without a Temporary Variable

The traditional swap uses an extra variable:

int tmp = x;
x = y;
y = tmp;

Bitwise XOR can perform the swap in place:

x = x ^ y   // (1)
y = x ^ y   // (2)
x = x ^ y   // (3)

3. Find the Number That Appears Only Once

Given an array where every number appears twice except one, XORing all elements isolates the unique value.

int find(arr){
int tmp = arr[0];
for(int i = 1;i < arr.length; i++){
tmp = tmp ^ arr[i];
}
return tmp;
}

This runs in O(n) time and O(1) extra space.

4. Fast Exponentiation Using Bitwise Operations

Instead of multiplying m by itself n times, exponentiation by squaring uses the binary representation of n.

int pow(int n){
int sum = 1;
int tmp = m;
while(n != 0){
if(n & 1 == 1){
sum *= tmp;
}
tmp *= tmp;
n = n >> 1;
}
return sum;
}

The algorithm runs in O(log n) time.

5. Find the Largest Power of Two Not Greater Than N

A simple loop multiplies by two until the next multiplication would exceed N:

int findN(int N){
int sum = 1;
while(true){
if(sum * 2 > N){
return sum;
}
sum = sum * 2;
}
}

The bitwise version spreads the highest set bit to the right and then shifts back:

n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8 // 整型一般是 32 位,上面我是假设 8 位。
return (n + 1) >> 1;

This runs in near‑constant time and showcases the power of binary manipulation.

Conclusion

The five examples illustrate that bitwise techniques often provide faster, more compact solutions and can add a “high‑coolness” factor to code, encouraging developers to consider binary operations whenever performance matters.

Javaperformanceoptimizationalgorithmbitwisecodingbinary
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.