forked from anku580/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryPalindrome.java
More file actions
35 lines (29 loc) · 816 Bytes
/
binaryPalindrome.java
File metadata and controls
35 lines (29 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class binaryPalindrome
{
static int isKthBitSet(long x, long k)
{
int rslt = ((x & (1 << (k - 1))) != 0) ? 1 : 0;
return rslt;
}
static int isPalindrome( long x)
{
long l = 1; // Initialize left position
long r = (Integer.SIZE/8 )* 8; // initialize right position
while (l < r)
{
if (isKthBitSet(x, l) != isKthBitSet(x, r))
{
return 0;
}
l++; r--;
}
return 1;
}
public static void main (String[] args)
{
long x = 1 << 15 + 1 << 16 ;
System.out.println(isPalindrome(x));
x = (1 << 31) + 1 ;
System.out.println(isPalindrome(x));
}
}