-1

I can't write negative decimal to binary c code. need help :) Here is my code...... There is no need other includes.

#include <stdio.h>

    int main(void){
        int i;
        int j;
        int b[16];
        printf("input number");
        scanf("%d",&i);
        if(i <= 0) {
            // here is negative decimal code
        } else {
            for(j=15;j>=0;j--) {
                b[j] = i % 2;
                i = i / 2;
            }
            for (j=0;j<=15;j++)
                printf("%d", b[j]);
            printf("\n");
        }
        return 0;
    }
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Jong Sun Choi
  • 13
  • 1
  • 1
  • 2
  • 3
    "// here is negative decimal code" ... i dont see any code for this case. Do you want someone to write it for you? – 463035818_is_not_an_ai Nov 09 '16 at 12:45
  • What are you trying to do? Build up an array of ints where each int is a binary digit? What's that good for? Any reason why you can't just do `for(uint32_t i=0; i – Lundin Nov 09 '16 at 12:48
  • To print the binary encoding of _any_ object, consider [this](http://stackoverflow.com/questions/35364772/how-to-print-memory-bits-in-c/35367414#35367414) – chux - Reinstate Monica Nov 09 '16 at 14:47

3 Answers3

2

Just view the integer as unsigned, and output the bits.

You will get the bits of the internal representation of the negative number (typically two's complement). No need to treat negative numbers separately.

Also, and this seems to be very hard to understand for many beginners, the number in i is not "decimal". It was initialized by reading a string of decimal digits, but the underlying computer memory is probably using a binary representation to store it.

The actual numbers are the same: for instance 2310 = 101112 means that the two entities are the same, they are equal. Viewing a number in a particular base doesn't change the number.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

You can use bitshift operator to get the bits.

#include <stdio.h>

    int main(void){
        int i            = 0;
        unsigned int u_i = 0;
        int j            = 0;
        int b[16]        = {0}; //Assuming your integer size is 16bit

        printf("input number");
        scanf("%d",&i);

        u_i = (unsigned int)i;

        for(j=15;j>=0;j--) {
            b[j] = u_i & 0x1;
            u_i  = u_i >> 1;
        }

        for (j=0;j<=15;j++)
            printf("%d", b[j]);

        printf("\n");
        return 0;
    }
MayurK
  • 1,925
  • 14
  • 27
  • Right shifting a 16-bit int with a negative value results in implementation defined behavior [Ref](http://stackoverflow.com/questions/40508958/shifting-a-negative-signed-value-is-undefined/40509322#comment68260203_40508958) as this code does. This will likely give expected results, but not specified by C to do so. – chux - Reinstate Monica Nov 09 '16 at 14:44
  • @chux, I didn't understand that post completely, but I understand your point. I modified my code and using unsigned int for shift operation. – MayurK Nov 11 '16 at 04:42
1

To print a signed integer as binary, perform repeated divisions by 2. No need to assume the width is 16 nor using 2's complement. Using >> shifts invites implementation defined behavior.

#include <stdio.h>
#include <stdbool.h>

void print2_helper(int x) {
  bool isodd = x%2;
  x /= 2;
  if (x) print2_helper(x);
  putchar('0' + isodd);
}

void print2(int x) {
  printf("Decimal:%d\tBinary:", x);
  if (x < 0) putchar('-');
  print2_helper(x);
  putchar('\n');
}

int main(void){
  print2(0);
  print2(1);
  print2(-1);
  print2(2);
  print2(-2);
  print2(INT_MAX);
  print2(INT_MIN);
  return 0;
}

Output.

Decimal:0   Binary:0
Decimal:1   Binary:1
Decimal:-1  Binary:-1
Decimal:2   Binary:10
Decimal:-2  Binary:-10
Decimal:2147483647  Binary:1111111111111111111111111111111
Decimal:-2147483648 Binary:-10000000000000000000000000000000
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256