Check if actual binary representation of a number is palindrome | Java code to check whether binary representation of a number is palindrome or not ; function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; function to check a number is palindrome or not ; get the number by reversing bits in the binary representation of ' n ' ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static long reverseBits ( long n ) { long rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( ( n & 1 ) == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } public static boolean isPalindrome ( long n ) { long rev = reverseBits ( n ) ; return ( n == rev ) ; } public static void main ( String argc [ ] ) { long n = 9 ; if ( isPalindrome ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }