检查数字的二进制表示是否为回文

给定一个整数“x”,编写一个C函数,如果x的二进制表示形式是回文形式,则返回true,否则返回false。 例如,二进制表示为10的数字。。01是回文数字,二进制表示为10。。00不是回文。 这个想法类似于 检查字符串是否为回文 .我们从最左边和最右边的位开始,逐个比较位。如果发现不匹配,则返回false。

null

算法: isPalindrome(x) 1) 使用sizeof()运算符查找x中的位数。 2) 将左侧和右侧位置分别初始化为1和n。 3) 当左侧的“l”小于右侧的“r”时,执行以下操作。 ..…..a) 如果位置“l”处的位与位置“r”处的位不同,则返回false。 ..…..b) 递增“l”和递减“r”,即do l++和r–。 4) 如果我们到了这里,这意味着我们没有发现任何不匹配的地方。 为了在给定位置找到位,我们可以使用类似于 邮递表达方式“ x和(1< “如果设置了从右侧开始的第k位,则给出非零值;如果未设置第k位,则给出零值。”。

下面是上述算法的实现。

C++

// C++ Program to Check if binary representation
// of a number is palindrome
#include<iostream>
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

<?php
// PHP Program to Check if binary representation
// of a number is palindrome
// 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
function isKthBitSet( $x , $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
function isPalindrome( $x )
{
$l = 1; // Initialize left position
$r = sizeof(4) * 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
$x = 1 << 15 + 1 << 16;
echo isPalindrome( $x ), "" ;
$x = 1 << 31 + 1;
echo isPalindrome( $x ), "" ;
// This code is contributed by ajit.
?>


Javascript

<script>
// Javascript program to Check if binary
// representation of a number is palindrome
// 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
function isKthBitSet(x, k)
{
let 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
function isPalindrome(x)
{
// Initialize left position
let l = 1;
// initialize right position
let r = 4 * 8;
// One by one compare bits
while (l < r)
{
if (isKthBitSet(x, l) !=
isKthBitSet(x, r))
{
return 0;
}
l++; r--;
}
return 1;
}
// Driver code
let x = 1 << 15 + 1 << 16;
document.write(isPalindrome(x) + "</br>" );
x = (1 << 31) + 1;
document.write(isPalindrome(x));
// This code is contributed by divyesh072019
</script>


输出:

11

时间复杂度:O(x)

辅助空间:O(1)

本文由 苏拉布·古普塔 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享