Skip to content

feat: Add BinaryPalindromeCheck new algorithm with Junit tests #5708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}