Sara Tine
Sara Tine

Reputation: 39

My binary is outputting in reverse order

I'm writing a program that takes a users input of characters, singles each character, converts each to binary, and also counts the amount of '1's in each binary. So far, I have a working code. However, the output of each binary is in reverse order. Like this:

The character A = 10000010 1's = 2

When what I want/need is:

The character A = 01000001 1's = 2

I am required to use the amount of functions I already have and have been told that fixing this will be as simple as doing recursion on my binaryPrinter function. I'm confused about where I would do that within my function and what arguments I would send through. Any help would be fantastic, thanks.

p.s. I'm required to use recursion in the binaryPrinter function to loop the program which apparently is going to solve my backwards binary problems if I place it in the right part of the binaryPrinter function.

#include <stdio.h>
#include <stdlib.h>

void binaryPrinter(int digitsLeft, int value, int * numberOfOnes);
void print(char c);
int charToInt(char C)
{
    int c;

    c=C;    

    return (int) c;
}
int main ()
{
    char value;
    int result = 1;
    while (result != EOF)
        {
            result = scanf("%c", &value);
            if (result != EOF)
                {
                    print(value);
                }
        }

}

void print(char c)
{
    int digits=8, value=c;
    int  ones=0;

    printf("The character %c = ", c);

    binaryPrinter(digits, value, &ones );

    printf(" 1's = %d\n", ones);
}

void binaryPrinter(int digitsLeft, int value, int * numberOfOnes)
{

    for (digitsLeft=8; digitsLeft>0; digitsLeft--)
        {
            if( value & 1 )
                {
                    printf("1");
                    *numberOfOnes=*numberOfOnes+1;
                }
            else
                {
                    printf("0");
                }
            value = value >> 1;

        }
}

Upvotes: 0

Views: 388

Answers (3)

Barmar
Barmar

Reputation: 780869

Here's the recursive version. Notice that it recurses before printing anything, so it prints as it's returning from processing each digit, which prints them in the proper order.

void binaryPrinter(int digitsLeft, int value, int * numberOfOnes)
{
    if (digitsLeft > 1) {
        binaryPrinter(digitsLeft - 1, value >> 1, numberOfOnes);
    }

    if (value & 1) {
        printf("1");
        (*numberOfOnes)++;
    } else {
        printf("0");
    }

}

Upvotes: 1

Marc B
Marc B

Reputation: 360612

Well, you are looking at the digits in reverse order:

if (value & 1) --> take the right-most digit
value = value >> 1; --> shift to the right

so if you have

12345678

(yes, that's not bits, this is just to illustrate the positioning) for inputs, you're doing:

8,7,6,5,4,3,2,1

for output. You need to reverse the logic by working from the left (highest) bits in your original string and work yourway down to the least significant bits.

Upvotes: 0

Change the loop to

for (digitsLeft=1; digitsLeft<=8; digitsLeft++)

Upvotes: 0

Related Questions