Skip to content

Commit e1c568f

Browse files
committed
Fix
1 parent c15b966 commit e1c568f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ private SwapAdjacentBits() {
4141
* @return the integer after swapping every pair of adjacent bits
4242
*/
4343
public static int swapAdjacentBits(int num) {
44+
// mask the even bits (0xAAAAAAAA => 10101010...)
4445
int evenBits = num & 0xAAAAAAAA;
46+
47+
// mask the odd bits (0x55555555 => 01010101...)
4548
int oddBits = num & 0x55555555;
49+
50+
// right shift even bits and left shift odd bits
4651
evenBits >>= 1;
4752
oddBits <<= 1;
53+
54+
// combine shifted bits
4855
return evenBits | oddBits;
4956
}
5057
}

0 commit comments

Comments
 (0)