0

I have written a program that will convert a number into string. The core logic is using % 10. However, I am looking for some other way. The other way should be using bitwise operator. The first question comes to my mind is how to split the digits using bitwise operation. I am unable to think on those lines. Here is my usual program.

   #include "stdio.h"

void itoaperChar(int n, char *s)
{
    s[0] = '0' + n;

    s[1] = '\0';
}

typedef struct my_string
{
    char val;
    struct my_string *next;
}my_string;

void convertitoString(int nu, char *des)
{
    char tempChar[2];
    my_string *Head = NULL;
    my_string *tempNode = NULL;

    while( nu != 0)
    {
        /** when we apply the logic of traversing from last, the data is accessed as LIFO **/
        /** we are again applying LIFO to make it FIFO **/
        int temp = nu%10;
        itoaperChar(temp,&tempChar);
        if(Head == NULL )
        {
            Head =(my_string*)malloc(sizeof(my_string));

            /**  Remember, strcpy looks for \0 in the source string. Always, ensure that the string is null terminated. Even if the string is just 1 byte.**/
            strcpy(&(Head->val),tempChar);
            Head->next = NULL;
        }
        else
        {
            tempNode = (my_string*)malloc(sizeof(my_string));
            strcpy(&(tempNode->val),tempChar);
            tempNode->next = Head;
            Head = tempNode;
        }

        nu = (nu - temp)/10;

    }

    int lcindex = 0;

    while(Head!=NULL)
    {

        strcpy(&(des[lcindex]),&(Head->val));
        lcindex++;
        tempNode = Head;
        Head=Head->next;
        free(tempNode);
        tempNode = NULL;
    }

    des[lcindex] = '\0';

}

void main()
{
    char t[10];
    convertitoString(1024,t);
    printf("The value of t is %s ", t);
}
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • 1
    Please indent your code properly. (Also, you seem to have forgotten to close a comment block.) – jwodder Apr 12 '14 at 03:50
  • Give this one some time - I suspect interesting answers. – chux - Reinstate Monica Apr 12 '14 at 04:31
  • I suspect a lot of face hitting desk and moving onto another question ... – M.M Apr 12 '14 at 04:55
  • Why do you say that they will hit their face at desk? – dexterous Apr 12 '14 at 06:33
  • Is this a homework assignment? Why would you be looking for a way to do this with bitwise operations? I would start by looking for a way to do the %10 version without creating a linked list and using strcpy to move 1 character strings around. YIKES! – pat Apr 12 '14 at 06:36
  • Yes, base 10 only. It is not a homework. I was thinking whether it is possible by bitwise or not. Because this modulus by 10 is what I have used in my 9th grade. Now, I am 31 years old. I don't know whether there is some other possibility or not? Or, I have just stuck with my conventional method. Looks like modulus is only the way. – dexterous Apr 12 '14 at 06:55
  • "other way should be using bitwise operator". Does this mean no adding? (as in `lcindex++;`) or arithmetic comparisons?. Otherwise the double dabble algorithm suggested by @Lưu Vĩnh Phúc looks good. A solution exist that only uses `| & << >> `, small table look-up. See [Binary-to-BCD Converter p. 8](http://www.utm.edu/~leeb/DM74185.pdf) – chux - Reinstate Monica Apr 12 '14 at 08:01

2 Answers2

2

I don't understand your code since it's so badly indented, but why making it so complex like that? A number-to-string program that uses divide-by-10 can take only a few (tens of) lines

If your want to do it with only bitwise operation, you can use double dabble algorithm. It converts a number from binary to BCD with only bitwise operations. The conversion from BCD to string is just an expansion from packed nibbles to 2 character bytes

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • That's cool, but the double dabble algorithm still involves arithmetic operations in addition to the bitwise ones. I guess I really don't understand what the OP is after... – pat Apr 12 '14 at 07:10
  • @pat there's almost no way to do complex arithmetics with only bitwise operations. You can replace additions and subtractions with software adders but that'll make the function slows down seriously – phuclv Apr 12 '14 at 09:49
1

Modulus is an arithmetic operator based on division; it's fundamentally an arithmetic operator, not a bitwise operator. Unless the modulus is a power of 2 — which it is not here — there is no simple way to implement it in terms of bitwise operators. Sorry.

  • technically it's difficult to do a division using only bitwise operators, but it's not impossible. It's much easier if you want to divide by a constant like 10 like this, by [multiplying with the multiplicative inverse](https://stackoverflow.com/q/5558492/995714) – phuclv Jul 04 '18 at 05:57
  • @phuclv While that’s a handy trick, multiplication isn’t a bitwise operation either. –  Jul 04 '18 at 06:22