i am trying to convert decimals to binary ..the reason i am using function is becoz i want to include -ve numbers too .......i was sucessfull for the positive but for making it run for -ve numbers to i divided into a new function....but i am not able to figure out when i return a pointer frm a function ...hw to print that out in the main function

here is the code:

#include<iostream>
#include<iomanip>

using namespace std;
int *unsign(int);



int main()
{
	int *bins;
	int num;

	cout<<"enter the number you want to convert:"<<endl;
	cin>>num;

	bins= unsign(num);

	int i=0;
// this is the place where i am stuck
	for(int i=0;i<5;i++)
	
		cout<<bins[i];
  
	

	delete []bins;
	bins=0;

	return 0;
}

int *unsign(int num)
{
	int number;
	int *bin;
	
		static int count =0;

	number=num;

	while (num>1)
	{
		num=num/2;
		count++;
	}
	count++;

	bin= new int[count];

	for(int i=0;i<count;i++)
	{
		bin[(count-1)-i]=number%2;

		if ((count-1)-i==0)
		{
			bin[0]=1;
		}
	}

	return bin;
}

Recommended Answers

All 7 Replies

I'm not sure what you mean when you speak of converting negative numbers.

A negative decimal number is represented in the computer by a system called Two's complement (see http://en.wikipedia.org/wiki/Two%27s_complement). In order to find the 2s complement you toggle the bits of the binary representation (1-> 0 and 0->1) and then add 1 (binary) to the result.

If what you are trying to do is be able to accept negative numbers from your user and just declare that -5 is -0101 (which as stated above is incorrect) then just test if the value entered is less than zero and multiply it by -1 and convert.

So, the method unsign confuses me a bit. Your logic for converting (positive) numbers seems to be fairly sound but I haven' tested it yet. To get some ideas (resist copying) for how other people have done it search on the site as this is a common problem (it seems having your own version is a prerequisite to register on DW!).

I'm not sure what you mean when you speak of converting negative numbers.

A negative decimal number is represented in the computer by a system called Two's complement (see http://en.wikipedia.org/wiki/Two%27s_complement). In order to find the 2s complement you toggle the bits of the binary representation (1-> 0 and 0->1) and then add 1 (binary) to the result.

If what you are trying to do is be able to accept negative numbers from your user and just declare that -5 is -0101 (which as stated above is incorrect) then just test if the value entered is less than zero and multiply it by -1 and convert.

So, the method unsign confuses me a bit. Your logic for converting (positive) numbers seems to be fairly sound but I haven' tested it yet. To get some ideas (resist copying) for how other people have done it search on the site as this is a common problem (it seems having your own version is a prerequisite to register on DW!).

I understood how to work this out:

#include<iostream>
#include<iomanip>

using namespace std;
int *unsign(int);



int main()
{
	int *bins;
	int num;

	cout<<"enter the number you want to convert:"<<endl;
	cin>>num;

	bins= unsign(num);

	

	cout<<"thank you"<<endl;

	

	delete []bins;
	bins=0;

	return 0;
}

int *unsign(int num)
{
	int number;
	int *bin;
	
	int count =0;

	number=num;

	while (num>1)
	{
		num=num/2;
		count++;
	}
	count++;
	
		cout<< count<<endl;

	bin= new int[count];

	for(int i=0;i<count;i++)
	{
		bin[(count-1)-i]=number%2;
		number=number/2;

		if ((count-1)-i==0)
		{
			bin[0]=1;
		}
	}

	for(int i=0;i<count;i++)
	{
		cout<<bin[i];
	}
	cout<<endl;
	return bin;
}

i just made the change of including FOR loop in my function. so now it runs f9.......THe ques i have is - signed magnitude of a number will vary with the precision user wants it in like 8bit or 12bit so what i should do to make my program general not limited to anything .......i am not able figure how can i complement numbers ......which is the base for working out signed, 1s ,2s........can you just give me a hint so that i can think in the right direction??

Are you able to access the return value from your function in the main program? [EDIT] I tested it out and it looks you can.[/EDIT] I had some trouble getting something meaningful back (like for your bins value in main()) but if you took care of it with the output in your function all the better. Great going! I think it had to do with the array in main being passed in before it was initialized.

2s complement is no big deal:

say I had the number 10 (assume a 32-bit integer)
00000000 00000000 00000000 00001010
take the 1s complement (toggle the bits)
11111111 11111111 11111111 11110101
add one to the 1s complement
11111111 11111111 11111111  11110110 
set a signed     int = 0xFFFFFFF6 (the above number in hex) and print it out you get -10

Are you able to access the return value from your function in the main program? [EDIT] I tested it out and it looks you can.[/EDIT] I had some trouble getting something meaningful back (like for your bins value in main()) but if you took care of it with the output in your function all the better. Great going! I think it had to do with the array in main being passed in before it was initialized.

2s complement is no big deal:

say I had the number 10 (assume a 32-bit integer)
00000000 00000000 00000000 00001010
take the 1s complement (toggle the bits)
11111111 11111111 11111111 11110101
add one to the 1s complement
11111111 11111111 11111111  11110110 
set a signed     int = 0xFFFFFFF6 (the above number in hex) and print it out you get -10

thanks!! you kinda gave me a way to go abt it...here's wht i did:

#include<iostream>
#include<iomanip>

using namespace std;
int *unsign(int);



int main()
{
	int *bins;
	int num;
	int i=0;
	char choice;

	cout<<"enter the number you want to convert:"<<endl;
	cin>>num;
	if(num>0)
	{
	bins= unsign(num);
	}
	else
	{
		bins=unsign(num);
			cout<<"unsigned is out of range"<<endl;

	cout<<"do u wa;t to knw its signed function? y fr yes n fr no"<<endl;
	cin>>choice;
	if(choice=='y' || choice=='Y')
	{
		if(bins[0]=1)
		{
			bins[0]=0;
		}
		else if(bins[0]=0)
		{
			bins[0]=1;
		}

		cout<<"the signed conversion is:"<<endl;

		while(bins[i]!='\0')
		{
			cout<<bins[i];
			i++;
		}
	}
	else
	cout<<"\n thank you"<<endl;

	}

	delete []bins;
	bins=0;

	return 0;
}

int *unsign(int num)
{
	int number;
	int *bin;
	
	int count =0;

	number=num;

	while (num>1)
	{
		num=num/2;
		count++;
	}
	count++;
	
		cout<< count<<endl;

	bin= new int[count];

	for(int i=0;i<count;i++)
	{
		bin[(count-1)-i]=number%2;
		number=number/2;

		if ((count-1)-i==0)
		{
			bin[0]=1;
		}
	}

	for(int i=0;i<count;i++)
	{
		cout<<bin[i];
	}
	cout<<endl;
	return bin;
}

It has a little problem it doesn't prints put anything for signed conversion:

i really cant figure out why, because an array should have a '/0' character at the end......and if it does not it should go in an infinite loop... but nothin of the two is happening!!

You toggled the bits perfectly but I don't see where you add in the 1... It won't be much work, just make sure to carry the 1 along (so like 01111 base 2 + 0001base two will be 1+1 = 10 (mark 0 down), carry the one 1+1 = 10, (mark 0 down) carry the one, etc. It doesn't usually propagate too far in. You can check your answers with a small program where you set an int to the value of your binary string in hex and then display it.

I find the above a bit confusing. What's wrong with having a function that takes an unsigned value and displays the bits? Doesn't that then do the 2's complement thing automagically?

#include <iostream>

/** http://www.daniweb.com/code/snippet216408.html */
void bits_uint(unsigned int value)
{
   unsigned int bit;
   for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )
   {
      std::cout << (value & bit ? '1' : '0');
   }
   std::cout << '\n';
}


int main()
{
   bits_uint(42);
   bits_uint(-5);
}

/* my output
00000000000000000000000000101010
11111111111111111111111111111011
*/

DS, I completely agree. I was attempting to help the OP in the context of his assignment. I wasn't sure what if any restrictions were in place but I made some assumptions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.