0

I want to return a 16BIT binary from a decimal number. I havt 2 Codes.
In the first one I tried to return a char array.
In the second one a have an output but it in the inverse order.
Maybe someone take a look at the codes. Thanks

Output first Function: Þ0000000000010000es (x862uÿ¼×

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

const int BIT = 16;

void binary(int);
char *bin(int);


int main() {

    binary(16);
    printf("\n");
    printf("%s\n", bin(16));

    return 0;
}


char *bin(int x) {

    char * new = (char*)malloc(sizeof(char) * BIT + 1);
    new[BIT] = '\0';

    if (x >= 0 && x <= 65535) {
        for (int i = 0; i < BIT; i++) {
            if (x % 2 == 0) {
                new[BIT - i] = '0';
            } else {
                new[BIT - i] = '1';
            }
            x = x / 2;
        }
    }

    return new;
}


void binary(int x) {

    if (x >= 0 && x <= 65535) {
        for (int i = 0; i < BIT; i++) {
            if (x % 2 == 0) {
                printf("0");
            } else {
                printf("1");
            }
            x = x / 2;
        }
    }
}
Paul Nie
  • 21
  • 5
  • 1
    You can't return a pointer to an automatic array, since the storage is no longer allocated upon return. Either pass in the array from the caller, or else use `malloc` to allocate it. – Tom Karzes Jan 18 '18 at 12:21
  • In your second function, first find the highest non-zero bit as your starting point, then output the number from left-to-right. – Tom Karzes Jan 18 '18 at 12:23
  • what you mean with highest non zero bit? – Paul Nie Jan 18 '18 at 12:32
  • Your binary number may be viewed as a sequence of 0 and 1 bits. The highest non-zero bit is just the left-most non-zero bit. Start with the left-most non-zero bit, then output the bits to the right of it in sequence. If your number if 11010, start with the left-most 1, then output the bits in order from left to right: 1, then 1, then 0, then 1, then 0. – Tom Karzes Jan 18 '18 at 12:34
  • 1
    @PaulNie Maybe you need to ask a specific question. Your question now is "Maybe someone take a look at the codes" - this is not specific. Try to express in your question what you want to accomplish, and what you managed to do. Then people can fill in the rest. Also, you wrote two pieces of code to do the same task - why? Was there any problem with the first piece of code that you tried to fix by rewriting it? If yes, what the problem was? You can [edit] your question to specify these details. – anatolyg Jan 18 '18 at 12:47
  • I am learning C and always try to solve a problem in 2 different ways – Paul Nie Jan 18 '18 at 12:50
  • Please read and understand [the question on why not to cast the return value of `malloc()` and family in C](/q/605845). Also, you should know that `sizeof (char)` can only be 1, so that multiplication is pointless. – Toby Speight Jan 18 '18 at 18:30

2 Answers2

1

I try to answer your question. I am not interested in anybody down-voting me... So I do my very best!!

  1. If you want a binary representation of a decimal value, the easiest thing is to check for bit 0 and then shift the number as long as it is non-zero. Shifting one bit to the right is the same as dividing it by 2.
  2. Instead of checking if the number is between 0 and 65535, it is enough to look if it is zero if you want to abort. If you want a fixed amount of shifts, you can also count from 1 to 16.

So I would suggest something like this:

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

This would output zeros and ones in inversed direction until there are no more ones.

If you want a fixed width representation:

#define BITS 16

for (int bits = 0; bits < BITS; bits++) {
  if (number & 1) {
       printf("1");
   } else {
       printf("0");
   }
   number >>= 1;
}

And if you want to store it into an array, replace printf by

char *bitmap = (char*)malloc(BITS+1);
bitmap[BITS] = '\0';  // NULL termination

bitmap[BITS-1-bits] = '1';

or

bitmap[BITS-1-bits] = '0';

respectively and have

printf("%s\n", bitmap);

Don't forget to free(bitmap).

Always consider, that an array is zero-based indexed. So it is better to have bit numbering go from 0 to 15 in case of 16 bits. I have not tested this code but it works like this. :)

andi8086
  • 394
  • 2
  • 9
0

this works, function bin() overwrites array arr in main() ( How do I return a char array from a function? ):

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

const int BIT = 16;

void binary(int);
void bin(char*, int);


int main(int argc, char* argv[]) {

    int x ;

    x = atoi(argv[1]);

    char arr[BIT+1];

    //binary(16);

    bin(arr, x);   // functions overwrites array arr

    arr[BIT] = '\0';   // strings have to be null-terminated in C

    printf("%s \n", arr);

    return 0;
}

void bin(char *arr1,int x) {


    if (x >= 0 && x <= 65535)
      {
        for (int i = BIT-1; i >= 0; i--) 
              {
            if (x % 2 == 0) 
                  {
                    arr1[i] = '0';
                  } else 
                  {
                    arr1[i] = '1';
                  }
            x = x / 2;
               }

      }

}
ralf htp
  • 9,149
  • 4
  • 22
  • 34