Repository: TheAlgorithms/Java
Pull request #5708 (2024-10-10 14:53:48)

feat: Add `BinaryPalindromeCheck` new algorithm with Junit tests
<!--
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`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5708?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 59.75%. Comparing base [(`90d20b3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/90d20b3a43642548ecc55e425517583080a81e2a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`93af027`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/93af027541bcf3634acf0b85381dad1a3a4c9ab1?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    #5708      +/-   ##
============================================
+ Coverage     59.71%   59.75%   +0.03%     
- Complexity     3855     3860       +5     
============================================
  Files           561      562       +1     
  Lines         16017    16025       +8     
  Branches       3073     3075       +2     
============================================
+ Hits           9565     9576      +11     
+ Misses         6047     6046       -1     
+ Partials        405      403       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5708?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/DIRECTORY.md b/DIRECTORY.md
index 3692b436..b62cde03 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -23,6 +23,7 @@
             * [WordPatternMatcher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java)
             * [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java)
           * bitmanipulation
+            * [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
             * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
             * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
             * [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
@@ -650,6 +651,7 @@
             * [WordPatternMatcherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java)
             * [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java)
           * bitmanipulation
+            * [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
             * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
             * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
             * [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java b/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java
new file mode 100644
index 00000000..0d6fd140
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java
@@ -0,0 +1,43 @@
+package com.thealgorithms.bitmanipulation;
+
+/**
+ * This class contains a method to check if the binary representation of a number is a palindrome.
+ * <p>
+ *     A binary palindrome is a number whose binary representation is the same when read from left to right and right to left.
+ *     For example, the number 9 has a binary representation of 1001, which is a palindrome.
+ *     The number 10 has a binary representation of 1010, which is not a palindrome.
+ * </p>
+ *
+ * @author Hardvan
+ */
+public final class BinaryPalindromeCheck {
+    private BinaryPalindromeCheck() {
+    }
+
+    /**
+     * Checks if the binary representation of a number is a palindrome.
+     *
+     * @param x The number to check.
+     * @return True if the binary representation is a palindrome, otherwise false.
+     */
+    public static boolean isBinaryPalindrome(int x) {
+        int reversed = reverseBits(x);
+        return x == reversed;
+    }
+
+    /**
+     * Helper function to reverse all the bits of an integer.
+     *
+     * @param x The number to reverse the bits of.
+     * @return The number with reversed bits.
+     */
+    private static int reverseBits(int x) {
+        int result = 0;
+        while (x > 0) {
+            result <<= 1;
+            result |= (x & 1);
+            x >>= 1;
+        }
+        return result;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java
new file mode 100644
index 00000000..ff413442
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java
@@ -0,0 +1,18 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+public class BinaryPalindromeCheckTest {
+
+    @Test
+    public void testIsBinaryPalindrome() {
+        assertTrue(BinaryPalindromeCheck.isBinaryPalindrome(9)); // 1001 is a palindrome
+        assertFalse(BinaryPalindromeCheck.isBinaryPalindrome(10)); // 1010 is not a palindrome
+        assertTrue(BinaryPalindromeCheck.isBinaryPalindrome(0)); // 0 is a palindrome
+        assertTrue(BinaryPalindromeCheck.isBinaryPalindrome(1)); // 1 is a palindrome
+        assertFalse(BinaryPalindromeCheck.isBinaryPalindrome(12)); // 1100 is not a palindrome
+    }
+}