给定一个整数“ x”,编写一个C函数,如果x的二进制表示是回文,则返回true,否则返回false。
例如,二进制表示为10..01的数字是回文,二进制表示为10..00的数字不是回文。
这个想法类似于检查字符串是否为回文。我们从最左边和最右边的位开始,并逐位比较位。如果发现不匹配,则返回false。
算法:
isPalindrome(x)
1)使用sizeof()运算符查找x中的位数。
2)分别将左位置和右位置初始化为1和n。
3)在左“ l”小于右“ r”时进行跟随。
..…..a)如果位置“ l”处的位与位置“ r”处的位不同,则返回false。
..…..b)递增“ l”和递减“ r”,即,执行l ++和r–。
4)如果我们到达这里,则意味着我们没有找到不匹配的位。
为了找到给定位置的钻头,我们可以使用类似于本文的想法。如果设置了从右起第k个位置的位,则表达式“ x&(1 <<(k-1)) ”给出非零值;如果未设置第k个位,则表达式为零。
以下是上述算法的实现。
C++
// C++ Program to Check if binary representation
// of a number is palindrome
#include
using namespace std;
// This function returns true if k'th bit in x
// is set (or 1). For example if x (0010) is 2
// and k is 2, then it returns true
bool isKthBitSet(unsigned int x, unsigned int k)
{
return (x & (1 << (k - 1))) ? true : false;
}
// This function returns true if binary
// representation of x is palindrome.
// For example (1000...001) is palindrome
bool isPalindrome(unsigned int x)
{
int l = 1; // Initialize left position
int r = sizeof(unsigned int) * 8; // initialize right position
// One by one compare bits
while (l < r)
{
if (isKthBitSet(x, l) != isKthBitSet(x, r))
return false;
l++; r--;
}
return true;
}
// Driver Code
int main()
{
unsigned int x = 1 << 15 + 1 << 16;
cout << isPalindrome(x) << endl;
x = 1 << 31 + 1;
cout << isPalindrome(x) << endl;
return 0;
}
Java
// Java Program to Check if binary representation
// of a number is palindrome
class GFG
{
// This function returns true if k'th bit in x
// is set (or 1). For example if x (0010) is 2
// and k is 2, then it returns true
static int isKthBitSet(long x, long k)
{
int rslt = ((x & (1 << (k - 1))) != 0) ? 1 : 0;
return rslt;
}
// This function returns true if binary
// representation of x is palindrome.
// For example (1000...001) is palindrome
static int isPalindrome( long x)
{
long l = 1; // Initialize left position
long r = (Integer.SIZE/8 )* 8; // initialize right position
// One by one compare bits
while (l < r)
{
if (isKthBitSet(x, l) != isKthBitSet(x, r))
{
return 0;
}
l++; r--;
}
return 1;
}
// Driver Code
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));
}
}
// This code is contributed by AnkitRai01
Python3
# python 3 Program to Check if binary representation
# of a number is palindrome
import sys
# This function returns true if k'th bit in x
# is set (or 1). For example if x (0010) is 2
# and k is 2, then it returns true
def isKthBitSet(x, k):
if ((x & (1 << (k - 1))) !=0):
return True
else:
return False
# This function returns true if binary
# representation of x is palindrome.
# For example (1000...001) is palindrome
def isPalindrome(x):
l = 1 # Initialize left position
r = 2 * 8 # initialize right position
# One by one compare bits
while (l < r):
if (isKthBitSet(x, l) != isKthBitSet(x, r)):
return False
l += 1
r -= 1
return True
# Driver Code
if __name__ =='__main__':
x = 1 << 15 + 1 << 16
print(int(isPalindrome(x)))
x = 1 << 31 + 1
print(int(isPalindrome(x)))
# This code is contributed by
# Surendra_Gangwar
C#
// C# Program to Check if binary representation
// of a number is palindrome
using System;
class GFG
{
// This function returns true if k'th bit in x
// is set (or 1). For example if x (0010) is 2
// and k is 2, then it returns true
static int isKthBitSet(long x, long k)
{
int rslt = ((x & (1 << (int)(k - 1))) != 0) ? 1 : 0;
return rslt;
}
// This function returns true if binary
// representation of x is palindrome.
// For example (1000...001) is palindrome
static int isPalindrome( long x)
{
long l = 1; // Initialize left position
long r = 4 * 8; // initialize right position
// One by one compare bits
while (l < r)
{
if (isKthBitSet(x, l) != isKthBitSet(x, r))
{
return 0;
}
l++; r--;
}
return 1;
}
// Driver Code
public static void Main ()
{
long x = 1 << 15 + 1 << 16 ;
Console.WriteLine(isPalindrome(x));
x = (1 << 31) + 1 ;
Console.WriteLine(isPalindrome(x));
}
}
// This code is contributed by AnkitRai01
PHP
Javascript
输出:
1
1