简体   繁体   中英

Problem using shifting bit (check if a binary number is palindrome)

I need help with a code that check if a binary number is palindrome. Suppose that the input is 21 (10101). In the function i do that:

-copy the value of num1 to num2

-now num1 is 00010101 and num2 is 00010101

-shift(right) the bit of num1 while num1>0

-now num1 is: 00000000|10101000 and num2 is 00010101|00000000

-shift (left) only one position the bit of num1

-now num1 is: 00000001|01010000 and num2 is 00010101|00000000

-now while num1 != num2 i compare the bit and shift num1 left, and num2 right, each loop.

-compare example:

00000001|01010000

00010101|00000000
       |
       V
       1==1

next loop the compare is 0==0

So my code is the following. And i have a problem, because i dont know how to stop the last while (while with while condition: num1!=num2 && flag==true). Obviousbly the condition that i write is not the right way. But i dont how to do that. In this specific case i want to stop it if it checks 5 digit. An then i want to ask if the solution that i do is too difficult, and if there is other way to solve the problem (ps: its normal that i find it difficult? Because i thought to solve it transform the decimal number in binary manually using the while (n>0) -> n%2, than memorize the bit in an array, and do an easy algorithm on the array to check the reverse; but now i want to redo the problem using << and >> operator). Thank you all.

#include <iostream>

using namespace std;

int palindrome(unsigned short);

int main()
{
unsigned short num;

cout << "Inserisci un numero:\t";
cin >> num;

if (palindrome(num) == 1)
    cout << "Palindrome" << endl;
else
    cout << "Not palindrome" << endl;

cout << "\a";
return 0;
}

int palindrome(unsigned short num1)
{
unsigned short num2 = num1;
bool flag = true;

while (num1>0)
{
    num1 >>= 1;
}

num1 <<= 1;

while ((num1 != num2) && (flag == true))
{
    if ((num1 & 1) != (num2 & 1))
    {
        flag = false;
        break;
    }

    num1 <<= 1;
    num2 >>= 1;
}

return flag;
}

The solution you provide seems very complicated.
I would suggest a naive approach in which you just iterate the bits one by one starting from both ends of the integer.
As soon as there is a difference in these bit states, you can conclude it is not a palindrome.
If the middle of the integer is reached, then we can conclude it is a palindrome.

#include <stdio.h>
#include <stdbool.h>

bool
palindrome(unsigned short num)
{
  int bit_count=8*(int)sizeof(num);
  int half_bit_count=bit_count/2;
  for(int i=0; i<half_bit_count; ++i)
  {
    bool low=(num&(1u<<i))!=0;
    bool high=(num&(1u<<(bit_count-1-i)))!=0;
    if(low!=high)
    {
      return false;
    }
  }
  return true;
}

int
main(void)
{
  unsigned short i1=0x00A0;
  unsigned short i2=0x05A0;
  unsigned short i3=0xA005;
  unsigned short i4=0x005A;
  printf("%hx --> %d\n", i1, palindrome(i1));
  printf("%hx --> %d\n", i2, palindrome(i2));
  printf("%hx --> %d\n", i3, palindrome(i3));
  printf("%hx --> %d\n", i4, palindrome(i4));
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM