Repository: TheAlgorithms/Java
Pull request #5634 (2024-10-08 06:28:56)

Add `SwapAdjacentBits.java` new algorithm
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

Adds algorithm support for aglorithm 2 at #5628 


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5634?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 58.19%. Comparing base [(`9fb8192`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9fb819235643b8a6afa392b65e72a46036c4c210?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`def7a0b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/def7a0b0879a5965707d2e47c21cee5b9946ca5e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5634      +/-   ##
============================================
+ Coverage     58.17%   58.19%   +0.02%     
- Complexity     3687     3688       +1     
============================================
  Files           548      549       +1     
  Lines         15832    15837       +5     
  Branches       3012     3012              
============================================
+ Hits           9210     9217       +7     
+ Misses         6230     6229       -1     
+ Partials        392      391       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5634?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java
new file mode 100644
index 00000000..933dec56
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java
@@ -0,0 +1,26 @@
+package com.thealgorithms.bitmanipulation;
+
+/**
+ * Swap every pair of adjacent bits of a given number.
+ * @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
+ */
+
+public final class SwapAdjacentBits {
+    private SwapAdjacentBits() {
+    }
+
+    public static int swapAdjacentBits(int num) {
+        // mask the even bits (0xAAAAAAAA => 10101010...)
+        int evenBits = num & 0xAAAAAAAA;
+
+        // mask the odd bits (0x55555555 => 01010101...)
+        int oddBits = num & 0x55555555;
+
+        // right shift even bits and left shift odd bits
+        evenBits >>= 1;
+        oddBits <<= 1;
+
+        // combine shifted bits
+        return evenBits | oddBits;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java
new file mode 100644
index 00000000..67c98613
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java
@@ -0,0 +1,22 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+class SwapAdjacentBitsTest {
+
+    @ParameterizedTest
+    @CsvSource({
+        "2, 1", // 2 (10 in binary) should become 1 (01 in binary)
+        "43, 23", // 43 should become 23
+        "153, 102", // 153 should become 102
+        "15, 15", // 15 (1111) remains 15 (1111)
+        "0, 0" // 0 (0000) remains 0 (0000)
+    })
+    void
+    testSwapAdjacentBits(int input, int expected) {
+        assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input));
+    }
+}