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;
}