if you meant starting the leftmost 1, to check if its palindrome:
unsigned int o = v;
unsigned int r;
for (r = 0; v; v >>= 1)
{
r <<= 1;
r |= v & 1;
}
if (o == r)
printf("palindrome\n");
else
printf("not a palindrome\n");
On Jan 12, 5:50 am, awesomeandroid <[email protected]> wrote:
> @azhar good solution
>
> Thanks and Regards
> Priyaranjan
> code-forum.blogspot.com
>
> On Dec 22 2010, 1:55 pm, Azhar Hussain <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > Here is the simple solution
>
> > unsigned int r = v; // r will be reversed bits of v; first get LSB of v
> > int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
>
> > for (v >>= 1; v; v >>= 1)
> > {
> > r <<= 1;
> > r |= v & 1;
> > s--;
> > }
> > r <<= s; // shift when v's highest bits are zero
> > if (v == r)
> > printf("palindrome\n");
> > else
> > printf("not a palindrome\n");
>
> > source and more optimized
> > versionshttp://www-graphics.stanford.edu/~seander/bithacks.html#BitReverseObv...
>
> > -
> > Azhar.
>
> > On Wed, Dec 22, 2010 at 2:09 PM, pacific pacific
> > <[email protected]>wrote:
>
> > > On Wed, Dec 22, 2010 at 12:11 PM, mohan@ismu <[email protected]> wrote:
>
> > >> if x is a 32 bit number
> > >> if((x&0x0000FFFF)==((x>>16)&0x0000FFFF)))
> > >> x's bit pattern is a polyndrome
>
> > >> @snehal :Do you want to consider binary representation of 5 as 101 or
> > > 0000..0101 ?
> > > --
>
> > >> You received this message because you are subscribed to the Google Groups
> > >> "Algorithm Geeks" group.
> > >> To post to this group, send email to [email protected].
> > >> To unsubscribe from this group, send email to
> > >> [email protected]<algogeeks%2Bunsubscribe@googlegroups
> > >> .com>
> > >> .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/algogeeks?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Algorithm Geeks" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<algogeeks%2Bunsubscribe@googlegroups
> > > .com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.