1

I have a function which converts an integer to its binary representation and its stored in a long variable. My problem is that my function converts only positive integers so I need to implement a new function which will be slightly changed to do just that. I still need to store the bin. rep. in a long variable because that depends on the other features. Is there a way?

My function which successfully converts only positive integers:

long convertToBin(int decn)
{
  long binn = 0;
  long rem;
  long a = 1;
  while(decn != 0)
  {
    rem = decn % 2;
    binn = binn + rem * a;
    a = a * 10;
    decn = decn / 2;
  }
return binn;
}

I've tried it like this, but there is something wrong - doesn't work...

long negConvertToBin(int decn)
{
  long binn = 0;
  decn = abs(decn);
  decn = decn - 1;
  decn = ~decn;
  long rem;
  long a = 1;
  while(decn != 0)
  {
      rem = decn % 2;
      binn = binn + rem * a;
      a = a * 10;
      decn = decn / 2;
  }

return binn;
}
jirick
  • 71
  • 6
  • On some platforms `long` is only 32 bits, it can not itself store the binary representation of another 32-bit type (like `int` usually is) as it's much to large. If you want to display binary values, I recommend you use strings instead, with one character per binary digit (plus the null-terminator of course). – Some programmer dude Nov 07 '18 at 16:30
  • It'll be used just for smaller integers, so there should not be a problem.... – jirick Nov 07 '18 at 16:36
  • I tried it, but there was some problem... Could you please just modify my functional function for using string? I mean returning string... – jirick Nov 07 '18 at 16:38
  • When you say "smaller integers" what do you mean by that? How many bits do you need? – Some programmer dude Nov 07 '18 at 16:42
  • As for your problem, treat even negative numbers as unsigned. It will make it much easier to get the bits out of the numbers (but then you might get a *lot* more bits than can fit in your `binn` variable). – Some programmer dude Nov 07 '18 at 16:44
  • @Someprogrammerdude "with one character per binary digit (plus the null-terminator of course)." is off-by-1 for negative values as typical `INT_MIN` needs a 34 byte string. `"-10000000000000000000000000000000"`. – chux - Reinstate Monica Nov 07 '18 at 16:57

2 Answers2

1

Is there a way?

Consider code re-use since you have a working convertToBin() for positive values.

long NegAndPosConvertToBin(int decn) {
  if (decn < 0) return -convertToBin(-decn);
  return convertToBin(decn);
}

If OP still wants a stand-alone long negConvertToBin(int decn), then

long negConvertToBin(int decn) {
  decn = -decn;

  // same body as convertToBin() without the return

  return -binn;
}

As well commented by @Some programmer dude, OP's approach has limited useful int range as overflow occur with large values.


For a textual conversion for all int to various bases including 2, see char* itostr(char *dest, size_t size, int a, int base)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

i can't comment but sprintf function do works for you :

#include <stdio.h>

int main ( void )
{
    char temp[128] ;
    int num = -1  ;
    sprintf  ( temp , "%x" , num );
    puts ( temp ) ;
    return 0 ;
}