We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c15b966 commit e1c568fCopy full SHA for e1c568f
src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java
@@ -41,10 +41,17 @@ private SwapAdjacentBits() {
41
* @return the integer after swapping every pair of adjacent bits
42
*/
43
public static int swapAdjacentBits(int num) {
44
+ // mask the even bits (0xAAAAAAAA => 10101010...)
45
int evenBits = num & 0xAAAAAAAA;
46
+
47
+ // mask the odd bits (0x55555555 => 01010101...)
48
int oddBits = num & 0x55555555;
49
50
+ // right shift even bits and left shift odd bits
51
evenBits >>= 1;
52
oddBits <<= 1;
53
54
+ // combine shifted bits
55
return evenBits | oddBits;
56
}
57
0 commit comments