File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
main/java/com/thealgorithms/bitmanipulation
test/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 2323 * [ WordPatternMatcher] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java )
2424 * [ WordSearch] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java )
2525 * bitmanipulation
26+ * [ BinaryPalindromeCheck] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java )
2627 * [ BitSwap] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java )
2728 * [ ClearLeftmostSetBit] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java )
2829 * [ CountLeadingZeros] ( https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java )
650651 * [ WordPatternMatcherTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java )
651652 * [ WordSearchTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java )
652653 * bitmanipulation
654+ * [ BinaryPalindromeCheckTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java )
653655 * [ BitSwapTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java )
654656 * [ ClearLeftmostSetBitTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java )
655657 * [ CountLeadingZerosTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java )
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .bitmanipulation ;
2+
3+ /**
4+ * This class contains a method to check if the binary representation of a number is a palindrome.
5+ * <p>
6+ * A binary palindrome is a number whose binary representation is the same when read from left to right and right to left.
7+ * For example, the number 9 has a binary representation of 1001, which is a palindrome.
8+ * The number 10 has a binary representation of 1010, which is not a palindrome.
9+ * </p>
10+ *
11+ * @author Hardvan
12+ */
13+ public final class BinaryPalindromeCheck {
14+ private BinaryPalindromeCheck () {
15+ }
16+
17+ /**
18+ * Checks if the binary representation of a number is a palindrome.
19+ *
20+ * @param x The number to check.
21+ * @return True if the binary representation is a palindrome, otherwise false.
22+ */
23+ public static boolean isBinaryPalindrome (int x ) {
24+ int reversed = reverseBits (x );
25+ return x == reversed ;
26+ }
27+
28+ /**
29+ * Helper function to reverse all the bits of an integer.
30+ *
31+ * @param x The number to reverse the bits of.
32+ * @return The number with reversed bits.
33+ */
34+ private static int reverseBits (int x ) {
35+ int result = 0 ;
36+ while (x > 0 ) {
37+ result <<= 1 ;
38+ result |= (x & 1 );
39+ x >>= 1 ;
40+ }
41+ return result ;
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .bitmanipulation ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertFalse ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
6+ import org .junit .jupiter .api .Test ;
7+
8+ public class BinaryPalindromeCheckTest {
9+
10+ @ Test
11+ public void testIsBinaryPalindrome () {
12+ assertTrue (BinaryPalindromeCheck .isBinaryPalindrome (9 )); // 1001 is a palindrome
13+ assertFalse (BinaryPalindromeCheck .isBinaryPalindrome (10 )); // 1010 is not a palindrome
14+ assertTrue (BinaryPalindromeCheck .isBinaryPalindrome (0 )); // 0 is a palindrome
15+ assertTrue (BinaryPalindromeCheck .isBinaryPalindrome (1 )); // 1 is a palindrome
16+ assertFalse (BinaryPalindromeCheck .isBinaryPalindrome (12 )); // 1100 is not a palindrome
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments