Where to Find Bit Manipulation Algorithms in TheAlgorithms/Java
Bit manipulation algorithms in TheAlgorithms/Java are centralized in the com.thealgorithms.bitmanipulation package, located at src/main/java/com/thealgorithms/bitmanipulation/, with comprehensive JUnit tests in the parallel src/test/java/com/thealgorithms/bitmanipulation/ directory.
TheAlgorithms/Java is a curated collection of computer science algorithms implemented in standard Java. For developers needing low-level bitwise operations, the repository offers a dedicated module containing stateless, thread-safe utility classes. These algorithms operate directly on Java's native 32-bit int type and follow a strict single-responsibility design pattern.
Package Location and Architecture
All bit manipulation source files reside under the Maven-standard path:
src/main/java/com/thealgorithms/bitmanipulation/
The corresponding test suite mirrors this structure in:
src/test/java/com/thealgorithms/bitmanipulation/
Each implementation is a final utility class with a private constructor, exposing only static methods. This guarantees stateless, thread-safe usage without requiring instantiation. Input validation is built-in; for example, methods accepting bit positions verify they fall within the valid 32-bit range [0, 31].
Core Bit Manipulation Categories
The repository organizes bit manipulation algorithms into functional groups. Every class isolates one specific operation, making discovery and maintenance straightforward.
Bit Reversal and Rotation
- ReverseBits — Reverses the order of bits in a 32-bit integer. Implementation:
src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java. - BitRotate — Performs circular left and right bit rotation. Implementation:
src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java.
Bit Counting and Parity
- CountSetBits — Calculates the total number of set bits from 1 to n using an O(log n) algorithm. Implementation:
src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java. - ParityCheck — Determines whether an integer has odd or even parity.
- HammingDistance — Computes the Hamming distance between two integers.
Bit Position Operations
- BitSwap — Swaps the values at two arbitrary bit positions. Implementation:
src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java.
Mathematical and Encoding Utilities
- IsPowerTwo — Detects if a number is a power of two using the bitwise trick
n & (n-1) == 0. - GrayCodeConversion — Converts between standard binary and Gray code representations.
Practical Code Examples
All utilities follow the same invocation pattern: ClassName.methodName(arguments). They accept and return primitive int values.
Reverse Bits of an Integer
int n = 43261596; // 0b00000010100101000001111010011100
int reversed = ReverseBits.reverseBits(n);
System.out.println(reversed); // prints 964176192 (0b00111001011110000010100101000000)
Count Set Bits from 1 to n
int n = 10;
int totalSetBits = CountSetBits.countSetBits(n);
System.out.println(totalSetBits); // prints 17
Swap Two Bit Positions
int value = 0b00101000; // 40
int swapped = BitSwap.bitSwap(value, 2, 5);
System.out.println(Integer.toBinaryString(swapped)); // prints "1010000"
Rotate Bits Left
int x = 0b0001_0110; // 22
int rotated = BitRotate.rotateLeft(x, 3);
System.out.println(Integer.toBinaryString(rotated)); // prints "1010_0010"
Testing and Validation
Every algorithm is backed by JUnit tests in src/test/java/com/thealgorithms/bitmanipulation/. For example, BitSwapTest.java validates correctness across edge cases, including boundary bit positions and negative integers. This parallel directory structure ensures that any modification to the source is immediately verified against expected bitwise behavior.
Summary
- Location: Bit manipulation algorithms reside in
src/main/java/com/thealgorithms/bitmanipulation/under the packagecom.thealgorithms.bitmanipulation. - Design Pattern: Final utility classes with private constructors and static methods provide stateless, thread-safe operations.
- Key Classes:
ReverseBits,BitRotate,CountSetBits,BitSwap,IsPowerTwo, andGrayCodeConversioncover the most common low-level operations. - Usage: Direct static invocation with
intparameters; no object instantiation required. - Testing: Comprehensive JUnit suite located in
src/test/java/com/thealgorithms/bitmanipulation/ensures reliability.
Frequently Asked Questions
Where exactly are the bit manipulation files located in the repository?
All bit manipulation implementations are centralized in the com.thealgorithms.bitmanipulation package. Source files are in src/main/java/com/thealgorithms/bitmanipulation/, and unit tests are in src/test/java/com/thealgorithms/bitmanipulation/.
How do I use these bit manipulation classes in my own Java project?
Import the specific utility class and call its static method directly. For example, invoke ReverseBits.reverseBits(n) to reverse an integer's bits. Because these classes are final with private constructors, you cannot instantiate them—simply use the static API.
Are the bit manipulation utilities thread-safe?
Yes. Every class is declared final and contains only static methods with no instance state, making them inherently thread-safe. They operate solely on method arguments without maintaining internal mutable data.
What types of bit operations are implemented?
The repository covers bit reversal, rotation, counting set bits, parity checks, Hamming distance calculation, bit position swapping, Gray code conversion, and power-of-two detection. Each operation is isolated in its own class following the single-responsibility principle.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →