Convert binary to palindrome function 
Author Message
 Convert binary to palindrome function

Having trouble creating a function that converts a 8-bit binary number into
a 16-bit palindrome using the C bitwise operators, such as Bitwise AND,
Bitwise OR,... Left shift <<.  I tried several attempts at using left shift,
<< SIZE - 1....

I was successful converting to palindrome using the strrev function.
Here is what I have. Works for 8-bit or 16-bit binary unsigned integers.

/*------------------------------------------------------------------------
 * This function converts a binary number into a palindrome using strrev()
 * Paramaters:    *binstr, *palin
 * Return Value:  palinstr
 *------------------------------------------------------------------------
 */

char * bintopalindrome(char * binstr, char * palinstr)

{
 char temp[SIZE +1];
     *temp = '\0';
     *palinstr = '\0';
     strcpy(temp, binstr);
     strcat(palinstr,strrev(temp));
     strcat(palinstr,binstr);
     return palinstr;

Quote:
}

Thanks for your advice,

Al

--



Fri, 25 Apr 2003 03:00:00 GMT  
 Convert binary to palindrome function

Quote:

> Having trouble creating a function that converts a 8-bit binary number into
> a 16-bit palindrome using the C bitwise operators, such as Bitwise AND,
> Bitwise OR,... Left shift <<.  I tried several attempts at using left shift,
> << SIZE - 1....

I would do this:

#define bin01010101 0x55
#define bin10101010 0xAA
#define bin11001100 0xCC
#define bin00110011 0x33
#define bin11110000 0xF0
#define bin00001111 0x0F

unsigned int
bin_to_palindrome (unsigned int x)
{
        unsigned int a = x & 0xFF;
        x = x>>1 & bin01010101  |  x<<1 & bin10101010;
        x = x>>2 & bin00110011  |  x<<2 & bin11001100;
        x = x>>4 & bin00001111  |  x<<2 & bin11110000;
        return  x<<8 | a;

Quote:
}

I am learning C and I have not test this, so I hope there is no language
error.
Please let me know about it.
Most probably, the most efficient method would be a table lookup.
This function might help you build it.

Regards,
Miguel
--



Sun, 27 Apr 2003 09:47:11 GMT  
 Convert binary to palindrome function

Quote:

> Having trouble creating a function that converts a 8-bit binary number into
> a 16-bit palindrome using the C bitwise operators, such as Bitwise AND,
> Bitwise OR,... Left shift <<.  I tried several attempts at using left shift,
> << SIZE - 1....

Imagine you have two stacks.  S1 is initially empty and S2
contains some data.  Now if you repeatedly POP the topmost item
from S2 and PUSH it to S1, until S2 becomes empty, in the end you
have moved all the data from S2 to S1 and reversed its order in
the process.  (The original top is now in the bottom, because it
was popped first and pushed first.)

You can use this algorithm.  Each item is a bit, and each stack
is an unsigned (don't forget that!) integer.  Operations are:

  PUSH:  stack = (stack << 1) | item;
  POP:   item = (stack & 1); stack >>= 1;

Because the integers don't remember how many bits are in the
stack, you need a separate counter.

After you have reversed the order of bits, you can form the
palindrome by combining the reversed value and the original value
with << and | operators.

There are numerous other ways to reverse a bit string.  One of
them moves several bits at a time, like this:

  abcdefgh
  badcfehg
  dcbahgfe
  hgfedcba

Quote:
> /*------------------------------------------------------------------------
>  * This function converts a binary number into a palindrome using strrev()
>  * Paramaters:    *binstr, *palin
>  * Return Value:  palinstr
>  *------------------------------------------------------------------------
>  */

I like stating the preconditions of the function in the comment:

  This function generates a palindrome from the string BINSTR and
  stores the result in the buffer PALINSTR.  BINSTR can be at
  most SIZE characters long, and PALINSTR must have room for
  2*strlen(BINSTR)+1 characters.  The palindrome contains BINSTR
  first reversed and then in original order.  The function
  returns PALINSTR.

Quote:
> char * bintopalindrome(char * binstr, char * palinstr)

As the function is not intended to change *binstr, you should
make that parameter point to const.

Quote:
> {
>  char temp[SIZE +1];
>      *temp = '\0';
>      *palinstr = '\0';
>      strcpy(temp, binstr);
>      strcat(palinstr,strrev(temp));
>      strcat(palinstr,binstr);
>      return palinstr;
> }

Clearing *temp is not necessary because you overwrite the string
with a new one anyway.  Clearing *palinstr is necessary if you
strcat to it, but if you change the first strcat to strcmp, you
needn't clear that either.

Also note that you can get rid of temp (and thus the SIZE
limitation) altogether if you take advantage of strrev()
reversing the string in place: copy binstr to palinstr, reverse
it there and append the rest.

(Extra exercise: write a portable implementation for the
non-standard strrev function, and call it something that doesn't
begin with "str".)
--



Sun, 27 Apr 2003 09:47:28 GMT  
 Convert binary to palindrome function

Quote:

>I tried several attempts at using left shift, << SIZE - 1....
> I was successful converting to palindrome using the strrev function.
> Here is what I have. Works for 8-bit or 16-bit binary unsigned integers.

No it doesn't; it operates on strings of text characters.
Since you didn't show us your attempts at binary operations,
we can't advise you about what you did wrong -- unless it is
that you don't know the difference between characters and bits.
--



Sun, 27 Apr 2003 09:47:42 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Palindrome for binary numbers

2. Command or function for converting hexadecimal to binary

3. convert function string to binary

4. Proposal: alt.binaries.examples.vb4 , alt.binaries.examples.vcpp , alt.binaries.examples.java

5. Converting to binary

6. Converting floats to binary

7. Converting binary to string?

8. Converting int to binary string

9. how to convert numbers in decimal to binary?

10. convert integer to binary

11. Problem converting binary file to ASCII using C

12. How to convert binary to decimal?

 

 
Powered by phpBB® Forum Software