Skip to content

Commit 0ae0173

Browse files
authored
Merge branch 'master' into patch-2
2 parents 01b3142 + fd14016 commit 0ae0173

File tree

4 files changed

+101
-18
lines changed

4 files changed

+101
-18
lines changed

src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Converts any Octal Number to a Binary Number
4+
* This class provides a method to reverse the bits of a 32-bit integer.
5+
* Reversing the bits means that the least significant bit (LSB) becomes
6+
* the most significant bit (MSB) and vice versa.
7+
*
8+
* Example:
9+
* Input (binary): 00000010100101000001111010011100 (43261596)
10+
* Output (binary): 00111001011110000010100101000000 (964176192)
11+
*
12+
* Time Complexity: O(32) - A fixed number of 32 iterations
13+
* Space Complexity: O(1) - No extra space used
14+
*
15+
* Note:
16+
* - If the input is negative, Java handles it using two’s complement representation.
17+
* - This function works on 32-bit integers by default.
18+
*
519
* @author Bama Charan Chhandogi
620
*/
7-
821
public final class ReverseBits {
922
private ReverseBits() {
1023
}
1124

25+
/**
26+
* Reverses the bits of a 32-bit integer.
27+
*
28+
* @param n the integer whose bits are to be reversed
29+
* @return the integer obtained by reversing the bits of the input
30+
*/
1231
public static int reverseBits(int n) {
1332
int result = 0;
1433
int bitCount = 32;

src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Swap every pair of adjacent bits of a given number.
5-
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
4+
* A utility class to swap every pair of adjacent bits in a given integer.
5+
* This operation shifts the even-positioned bits to odd positions and vice versa.
6+
*
7+
* Example:
8+
* - Input: 2 (binary: `10`) → Output: 1 (binary: `01`)
9+
* - Input: 43 (binary: `101011`) → Output: 23 (binary: `010111`)
10+
*
11+
* **Explanation of the Algorithm:**
12+
* 1. Mask even-positioned bits: Using `0xAAAAAAAA` (binary: `101010...`),
13+
* which selects bits in even positions.
14+
* 2. Mask odd-positioned bits: Using `0x55555555` (binary: `010101...`),
15+
* which selects bits in odd positions.
16+
* 3. Shift bits:
17+
* - Right-shift even-positioned bits by 1 to move them to odd positions.
18+
* - Left-shift odd-positioned bits by 1 to move them to even positions.
19+
* 4. Combine both shifted results using bitwise OR (`|`) to produce the final result.
20+
*
21+
* Use Case: This algorithm can be useful in applications involving low-level bit manipulation,
22+
* such as encoding, data compression, or cryptographic transformations.
23+
*
24+
* Time Complexity: O(1) (constant time, since operations are bitwise).
25+
*
26+
* Author: Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
627
*/
7-
828
public final class SwapAdjacentBits {
929
private SwapAdjacentBits() {
1030
}
1131

32+
/**
33+
* Swaps every pair of adjacent bits of a given integer.
34+
* Steps:
35+
* 1. Mask the even-positioned bits.
36+
* 2. Mask the odd-positioned bits.
37+
* 3. Shift the even bits to the right and the odd bits to the left.
38+
* 4. Combine the shifted bits.
39+
*
40+
* @param num the integer whose bits are to be swapped
41+
* @return the integer after swapping every pair of adjacent bits
42+
*/
1243
public static int swapAdjacentBits(int num) {
1344
// mask the even bits (0xAAAAAAAA => 10101010...)
1445
int evenBits = num & 0xAAAAAAAA;

src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,40 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
class ReverseBitsTest {
811

9-
@Test
10-
void testReverseBits() {
11-
assertEquals(0, ReverseBits.reverseBits(0));
12-
assertEquals(-1, ReverseBits.reverseBits(-1));
13-
assertEquals(964176192, ReverseBits.reverseBits(43261596));
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
void testReverseBits(int input, int expected) {
15+
assertEquals(expected, ReverseBits.reverseBits(input));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(
20+
// Edge case: All bits are 0
21+
Arguments.of(0, 0),
22+
23+
// Edge case: All bits are 1 (Two’s complement representation of -1)
24+
Arguments.of(-1, -1),
25+
26+
// Case with random number 43261596
27+
Arguments.of(43261596, 964176192),
28+
29+
// Case with maximum positive value for 32-bit integer
30+
Arguments.of(Integer.MAX_VALUE, -2),
31+
32+
// Case with minimum value (all bits 1 except the sign bit)
33+
Arguments.of(Integer.MIN_VALUE, 1),
34+
35+
// Case with a single bit set (2^0 = 1)
36+
Arguments.of(1, Integer.MIN_VALUE),
37+
38+
// Case with alternating bits: 0b101010...10 (in binary)
39+
Arguments.of(0xAAAAAAAA, 0x55555555));
1440
}
1541
}

src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
class SwapAdjacentBitsTest {
99

1010
@ParameterizedTest
11-
@CsvSource({
12-
"2, 1", // 2 (10 in binary) should become 1 (01 in binary)
13-
"43, 23", // 43 should become 23
14-
"153, 102", // 153 should become 102
15-
"15, 15", // 15 (1111) remains 15 (1111)
16-
"0, 0" // 0 (0000) remains 0 (0000)
17-
})
11+
@CsvSource({"2, 1", // 2 (binary: 10) -> 1 (binary: 01)
12+
"43, 23", // 43 (binary: 101011) -> 23 (binary: 010111)
13+
"153, 102", // 153 (binary: 10011001) -> 102 (binary: 01100110)
14+
"15, 15", // 15 (binary: 1111) -> 15 (binary: 1111) (no change)
15+
"0, 0", // 0 (binary: 0000) -> 0 (binary: 0000) (no change)
16+
"1, 2", // 1 (binary: 01) -> 2 (binary: 10)
17+
"170, 85", // 170 (binary: 10101010) -> 85 (binary: 01010101)
18+
"85, 170", // 85 (binary: 01010101) -> 170 (binary: 10101010)
19+
"255, 255", // 255 (binary: 11111111) -> 255 (binary: 11111111) (no change)
20+
"128, 64", // 128 (binary: 10000000) -> 64 (binary: 01000000)
21+
"1024, 2048",
22+
"-1, -1", // -1 (all bits 1) remains -1 (no change due to two's complement)
23+
"-2, -3", // -2 (binary: ...1110) -> -3 (binary: ...1101)
24+
"2147483647, -1073741825", "-2147483648, -1073741824"})
1825
void
1926
testSwapAdjacentBits(int input, int expected) {
2027
assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input));

0 commit comments

Comments
 (0)