Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1120
  • Last Modified:

palindromic bit patterns.

hi friends,

i want to implement a functionthat takes a 32-bit integer and returns true if its bit pattern is palindromic.I want to do bit by bit comparisions.I am able to check for the character arrays and integer numbers.can anybody help me out with the code in checking bits for palindromes.

  I have the idea like we can take a bit aray of size 32 and ierate from end comparing b01==b31,b02==b30 etc
but i am really not sure
0
rrepaka
Asked:
rrepaka
  • 6
  • 2
  • 2
1 Solution
 
jkrCommented:
That's not a big deal - you can do that e.g. like

bool is_palindromic(int byte) {

    for ( int i = 0; i < 16; ++i) {

        if ( (byte & (1 << i)) != (byte & (1 << ( 31 - i))) return false;
    }

    return true;
}
0
 
jkrCommented:
Sorry, to make that a little clearer:

#include <stdio.h>

bool is_palindromic(int byte) {

    for ( int i = 0; i < 16; ++i) {

        bool bit1 = (byte & (1 << i));
        bool bit2 = (byte & (1 << ( 31 - i)));

        if ( bit1 != bit2) return false;
    }

    return true;
}

void main () {

    int n1 = 0xc0000003;
    int n2 = 0xc0000004;

    printf ( "0x%8.8x is %s\n", n1, is_palindromic(n1) ? "a palindrom" : "not a plaindrom");
    printf ( "0x%8.8x is %s\n", n2, is_palindromic(n2) ? "a palindrom" : "not a plaindrom");
}
0
 
stefan73Commented:
Hi jkr,

this won't work, you need another shift, otherwise it'll return only 0 as a palindrome.

if ( (byte & (1 << i)) != ((byte & (1 << ( 31 - i)))>>(31-i)) return false;

Unfortunately, there is no logical XOR operator ^^  :-/

I wonder if there is a more efficient way to do this... Hmmm... Maybe with a small lookup table and macros?

like

#define MASK(bit) ((1<<bit) | (1<<(31-bit)))

bool is_palindromic(int byte) {
    static int masks[16]={
        MASK(0), MASK(1), ... , MASK(15)
    };

    int* mask=masks;
    for ( int i = 0; i < 16; ++i,++mask) {
        val = *mask & byte;
        if(val && (val != *mask)) return false;        
    }

    return true;
}



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
>>this won't work

Check the sample program:

C:\tmp\cc>bitpal
0xc0000003 is a palindrom
0xc0000004 is not a plaindrom

I would not have posted it without testing...
0
 
rrepakaAuthor Commented:
Hi i understood the logic but my question if i convert the value of n1 to decimal format it does not appear to be a palindrome.So how can i know by seeing the value whether it is palindrome or not
0
 
jkrCommented:
>> if i convert the value of n1 to decimal format it does not appear to be a palindrome

Yes, but the bit pattern (11000000000000000000000000000011) is - and that's what you were asking for...
0
 
jkrCommented:
BTW, if you want to do that for decimal values, try

bool is_palindrome( char* s)
{
char temp [ 20 ];
myReverse ( s, temp );
if ( !strcmp ( s, temp ))

 return true;
else
return false;
}

void reverse ( char* s1, char* s2)
 {
   int i, length;

   length = strlen(s1);

   for ( i = 0; i < length; ++i) s2[length - i] = s1[i];
 }

void main () {

int n = 123321;
char temp[20];

sprintf(temp,"%d",n);

printf ( "%8d is %s\n", n, is_palindrome(temp) ? "a palindrom" : "not a plaindrom");
}
0
 
rrepakaAuthor Commented:
Thank you very much i am very much comfortable with character and integer palindromes.and now i have understood the bit paterns also.sorry it was a silly question before.got confused............
thank you.
0
 
jkrCommented:
Thanx :o')

BTW, if your questions are answered, you should close this Q here.
0
 
stefan73Commented:
jkr,
> I would not have posted it without testing...
I was referring to your first post:

>  if ( (byte & (1 << i)) != (byte & (1 << ( 31 - i))) return false;

with i=0 and byte =  0xc0000003

byte & 1 = 1
byte & (1 << 31) = byte & 0x80000000 = 0x8000000

...which is not quite the same. Using bools, like in your second post, should work.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now