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;
}
I have written this C++ program but i have 2 problems with this it works just fine but, if you can help me?
I think I see. You are feeding it the digits 0, 0, 0, 0, 1, in this order, and you want it in reverse order. I see two solutions. One. Convert the 1s and 0s into strings "1" and "0", and prepend your additional digits onto what you have, saving the string before displaying, or "cout-ing" it.
Example. You want 365[base eight] and get 5, 6, and 3 in that order. A certain null string variable is replaced with "5", then "65", then "365".
Two. If you can handle it, count the number of digits each time, and divide by the appropriate power of ten before adding the next digit. For example, say you want 365[eight], and you get 5, 6, and 3 in that order. Take a real variable equal to 0, then add 5, then divide by 10 and add 6 to get 6.5, then repeat with the 3 to get 3.65, then multiply by 100.
Granted, the second one may get messy.
HTH.
P.S. This might be better put in Programming subsection.
Reply:your function is not converting to binary.
you are doing remainder math first 16 or 32 or any even number when done x%2 will return 0 it is only when you have divided it down to 1 that you get the 1 at the end and that will work for any number you put in.
Try using a string and placing each number in a refrence location then converting that string back to an integer.
pear
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment