C++ 如何检查整数的二进制表示是否是回文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/845772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the binary representation of an integer is a palindrome?
提问by yesraaj
How to check if the binary representation of an integer is a palindrome?
如何检查整数的二进制表示是否是回文?
回答by Christoph
Hopefully correct:
希望正确:
_Bool is_palindrome(unsigned n)
{
unsigned m = 0;
for(unsigned tmp = n; tmp; tmp >>= 1)
m = (m << 1) | (tmp & 1);
return m == n;
}
回答by rlbond
Since you haven't specified a language in which to do it, here's some C code (not the most efficient implementation, but it should illustrate the point):
由于您尚未指定执行此操作的语言,因此这里有一些 C 代码(不是最有效的实现,但应该说明这一点):
/* flip n */
unsigned int flip(unsigned int n)
{
int i, newInt = 0;
for (i=0; i<WORDSIZE; ++i)
{
newInt += (n & 0x0001);
newInt <<= 1;
n >>= 1;
}
return newInt;
}
bool isPalindrome(int n)
{
int flipped = flip(n);
/* shift to remove trailing zeroes */
while (!(flipped & 0x0001))
flipped >>= 1;
return n == flipped;
}
EDITfixed for your 10001 thing.
编辑修复了你的 10001 件事。
回答by Liran Orevi
Create a 256 lines chart containing a char and it's bit reversed char. given a 4 byte integer, take the first char, look it on the chart, compare the answer to the last char of the integer. if they differ it is not palindrome, if the are the same repeat with the middle chars. if they differ it is not palindrome else it is.
创建一个包含字符的 256 折线图,它是位反转字符。给定一个 4 字节整数,取第一个字符,在图表上查看,将答案与整数的最后一个字符进行比较。如果它们不同,则不是回文,如果它们与中间字符相同,则重复。如果它们不同,则不是回文,否则就是回文。
回答by Stephan202
Plenty of nice solutions here. Let me add one that is not the most efficient, but very readable, in my opinion:
这里有很多不错的解决方案。让我添加一个在我看来不是最有效但非常易读的代码:
/* Reverses the digits of num assuming the given base. */
uint64_t
reverse_base(uint64_t num, uint8_t base)
{
uint64_t rev = num % base;
for (; num /= base; rev = rev * base + num % base);
return rev;
}
/* Tells whether num is palindrome in the given base. */
bool
is_palindrome_base(uint64_t num, uint8_t base)
{
/* A palindrome is equal to its reverse. */
return num == reverse_base(num, base);
}
/* Tells whether num is a binary palindrome. */
bool
is_palindrome_bin(uint64_t num)
{
/* A binary palindrome is a palindrome in base 2. */
return is_palindrome_base(num, 2);
}
回答by Dingo
The following should be adaptable to any unsigned type. (Bit operations on signed types tend to be fraught with problems.)
以下应该适用于任何无符号类型。(有符号类型的位操作往往充满问题。)
bool test_pal(unsigned n)
{
unsigned t = 0;
for(unsigned bit = 1; bit && bit <= n; bit <<= 1)
t = (t << 1) | !!(n & bit);
return t == n;
}
回答by user3059007
int palidrome (int num)
{
int rev = 0;
num = number;
while (num != 0)
{
rev = (rev << 1) | (num & 1); num >> 1;
}
if (rev = number) return 1; else return 0;
}
回答by Divyam
The solution below works in python:
下面的解决方案适用于python:
def CheckBinPal(b):
b=str(bin(b))
if b[2:]==b[:1:-1]:
return True
else:
return False
def CheckBinPal(b):
b=str(bin(b))
if b[2:]==b[:1:-1]:
return True
else:
return False
where b is the integer
其中 b 是整数
回答by brut3f0rc3
I know that this question has been posted 2 years ago, but I have a better solution which doesn't depend on the word size and all,
我知道这个问题已经在 2 年前发布了,但是我有一个更好的解决方案,它不依赖于字数等等,
int temp = 0;
int i = num;
while (1)
{ // let's say num is the number which has to be checked
if (i & 0x1)
{
temp = temp + 1;
}
i = i >> 1;
if (i) {
temp = temp << 1;
}
else
{
break;
}
}
return temp == num;
回答by Hariprasadmce
bool PaLInt (unsigned int i, unsigned int bits)
{
unsigned int t = i;
unsigned int x = 0;
while(i)
{
x = x << bits;
x = x | (i & ((1<<bits) - 1));
i = i >> bits;
}
return x == t;
}
- Call PalInt(i,1) for binary pallindromes
- Call PalInt(i,3) for Octal Palindromes
- Call PalInt(i,4) for Hex Palindromes
- 为二元回文调用 PalInt(i,1)
- 为八进制回文调用 PalInt(i,3)
- 为十六进制回文调用 PalInt(i,4)
回答by Wajahat
In JAVA there is an easy way if you understand basic binary airthmetic, here is the code:
在JAVA中,如果您了解基本的二进制airthmetic,有一个简单的方法,这里是代码:
public static void main(String []args){
Integer num=73;
String bin=getBinary(num);
String revBin=reverse(bin);
Integer revNum=getInteger(revBin);
System.out.println("Is Palindrome: "+((num^revNum)==0));
}
static String getBinary(int c){
return Integer.toBinaryString(c);
}
static Integer getInteger(String c){
return Integer.parseInt(c,2);
}
static String reverse(String c){
return new StringBuilder(c).reverse().toString();
}