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;
        }
    }
}
7
  • 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. Commented Jan 18, 2018 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. Commented Jan 18, 2018 at 12:23
  • what you mean with highest non zero bit? Commented Jan 18, 2018 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. Commented Jan 18, 2018 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. Commented Jan 18, 2018 at 12:47

2 Answers 2

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. :)

Sign up to request clarification or add additional context in comments.

4 Comments

The first function looks good. I have changed the code but an issue in the output.
What's the issue?
I changed my code above. the output is: Þ0000000000010000PROCESSORS=8
I tested your code above and it worked. Output: ➜ ~ ./a.out 131 0000000010000011 ➜ ~ ./a.out 8 0000000000001000 ➜ ~ ./a.out 6 0000000000000110 ➜ ~ ./a.out 66 0000000001000010 ➜ ~ ./a.out 130 0000000010000010 ➜ ~
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;
               }

      }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.