?
Solved

palindromic bit patterns.

Posted on 2005-04-01
10
Medium Priority
?
1,105 Views
Last Modified: 2008-01-09
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
Comment
Question by:rrepaka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13684026
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
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 13684182
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
 
LVL 12

Expert Comment

by:stefan73
ID: 13684258
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 13684348
>>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
 

Author Comment

by:rrepaka
ID: 13684658
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
 
LVL 86

Expert Comment

by:jkr
ID: 13684678
>> 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
 
LVL 86

Expert Comment

by:jkr
ID: 13684724
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
 

Author Comment

by:rrepaka
ID: 13684858
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
 
LVL 86

Expert Comment

by:jkr
ID: 13686010
Thanx :o')

BTW, if your questions are answered, you should close this Q here.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13688985
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question