-2

I wrote simple code in C language (without libraries), but the result is good only when you read it from right to left, how to reverse it? I want the most simplified code as it is possible. My code below:

#include <stdio.h>

int main() {
    int number;

    printf("Changes from decimal to binary\n");
    printf("Enter the number: ");

    scanf("%d",&number);

    do{
        if(number % 2 == 1){
            printf("1");
        }else{
            printf("0");
        }
        number = number / 2;
    }
     while(number>0);

  return 0;
}
sg7
  • 6,108
  • 2
  • 32
  • 40
Dawid Kwiatoń
  • 157
  • 2
  • 13
  • 1
    Allocate a character array for the output; replace your "printf"s with code that places each character into that array, back to front; print the array at the end. – Lee Daniel Crocker Dec 08 '17 at 23:57
  • If you want to reuse the code you've already written you can just reverse the bits in the integer before printing it out using your function. https://stackoverflow.com/questions/746171/most-efficient-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c. Btw, your code uses the variable 'liczba' which is not defined anywhere, I think you meant to use 'number'. Also, there is a bug for negative numbers. Any negative number modulo 2 will give either 0 or -1, rather than 1. – Kyrill Dec 09 '17 at 00:04
  • the posted code is starting from the least significant digit of the value. Start from the most significant digit -or- write a small function to reverse a string 'inplace' (about 5 lines of code). Use google to search for a function that performs the desired reverse of a string – user3629249 Dec 09 '17 at 12:07

3 Answers3

0

You could store the binary number in an array and then print it backwards. So you would have for example int binaryDigits[1024]; and then instead of printing you will store the number in the array with a counter. Before the loop: int i = 0; and after that instead of printf("1"); and printf("0");, binaryDigits[i++] = 1 and binaryDigits[i++] = 0 respectively. Finally you could print the number in order with a for loop:

    for (i = i - 1; i >= 0; i--)
        printf("%d", binaryDigits[i])
Kys Plox
  • 774
  • 2
  • 10
  • 23
0

You could use recursion and print the digits as you fall off the stack. I changed number from int to unsigned int.

void print_dec_to_bin_hlp(unsigned int number)
{
    if(number > 0)
    {
        print_dec_to_bin_hlp(number/2);
        printf("%d", (number%2));
    }
}

void print_dec_to_bin(unsigned int number)
{
    if (number == 0) printf("%d", number);
    else print_dec_to_bin_hlp(number);
}
MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
0

A recursive solution using bitwise operations, for unsigned integers:

void print_binary(unsigned int num) {
    if(num >> 1) print_binary(num >> 1);
    putchar(num & 1 ? '1' : '0');
}

The bitwise operators >>, &, etc. access the bits in the value. The bits form a binary representation of the number. >> shifts the bits to the right, where the bits are ordered from MSB (most significant bit) to LSB, like the normal way numbers are written.

num & 1 returns non-zero only if the LSB (rightmost) bit is set. (AND mask with ..0001). So (num >> 1) & 1 does the same on the second-rightmost bit. So this recursive function prints the bits in MSB to LSB order.

The if(num >> 1) check makes sure prefix zeroes are not printed: If after the rightshift, num is all-zeroes, it is not printed any further. The check is made outside the function call, so that print_binary(0) (initial call) will still always print a single 0.

For signed integers:

void print_signed_binary(int num) {
    if(num < 0) {
        putchar('-');
        print_binary(-num);
    } else {
        print_binary(num);
    }
}
tmlen
  • 8,533
  • 5
  • 31
  • 84
  • Why in your first solution do you shift the number rather than the mask? And the recursive one will fail if the type is signed and you feed it a negative number, otherwise it's a very elegant solution. – Mark Ransom Dec 09 '17 at 03:33