|
-
Sep 25th, 2001, 07:41 PM
#1
Thread Starter
PowerPoster
Decmial to Binary (answer)
I just thought of putting this code up because some people were asking how to convert a decimal number to 8 bit binary number. So here is a little code:
PHP Code:
#include <iostream>
#include <string>
using namespace std;
/*****************************************************************
Thanks to everybody who helped me.
GOD BLESS THEM..
I am only 92:o
$$$$$$$$$$$$$$$$$$$$$$$$$$
ALL ABOVE COPIED FROM TOM's main coding comments:D;)
****************************************************************/
//Function to convert decmial number to its binary form
string dectobin(int what)
{
char bin[10] = "";
int dvs = 128;
while (dvs>=1)
{
if (what >= dvs)
{
what = what - dvs;
strcat(bin,"1");
}
else
{
strcat(bin,"0");
}
dvs = dvs/2;
}
return bin;
}
//An example of using the above function
int main()
{
int x;
cout<<dectobin(195)<<endl;
cin>>x;
return 0;
}
Like the title comments?
-
Sep 26th, 2001, 06:57 AM
#2
to speed it up, replace
dvs = dvs / 2; // shorter would be dvs /= 2;
with
dvs >>= 1;
The result is the same, yet bitshifting is faster than divisions.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|