Thursday, July 30, 2009

I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below

I have written this program to convert a integer to binary digit


problem one is that the binary conversion that i get is inverse i.e





if the conversion for 16 is


10000


but this program will output it as


00001


wut function can i use to convert my answer 00001 into 10000


should i use strings fuction get the length of the line than extact each string and use it to get 10000


Or is there a easy way that i can use


but i think the way i did is fine but the problem is that i am getting inverse answer waiting to hear from you thank u





#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


using namespace std;





int main ()


{


unsigned int a;


unsigned int y;


a=16;


y=a%2;


a=a/2;


while((a)!=0)


{


cout%26lt;%26lt;y;


y=a%2;


a=a/2;


}


cout%26lt;%26lt;y%26lt;%26lt;endl;





return 0;


}








If you dont understand any part of my question or my program u can ask me





I recently got an idea would it be better to use a bitwise operator


if so can you guide me a bit on using it and the function that i can use

I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below
you can do it via 2 approches ,





using the bitwise operator as you said, since the number are stored in binary form there is no explicity conversion required from decimal to binary, just spit out the bits,after masking them,





second use a stack, instead of cout %26lt;%26lt;y push y on the stack, and when your loop ends , write another loop ,poping out stored values, :)
Reply:use masking and shifting


e.g for two byte value start with 0x8000 mask it with the number (perfoming a bitwise and) , then shift right your mask and repeat the process,


loop this for 16 times for 2 byte no.


you can extend this approach for larger no too. Report It



No comments:

Post a Comment