Results 1 to 2 of 2

Thread: Decmial to Binary (answer)

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    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?
    Baaaaaaaaah

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width