整数十进制转二进制

 

#include <iostream>
#include <string>
using namespace std;

//整数十进制转二进制
string DecimalToBinary(int dec)
{
    string returnStr = "";
    while (dec)
    {
        if (dec & 0x01 == 1){
            returnStr.insert(0, "1");
            dec >>= 1;
        }
        else{
            returnStr.insert(0, "0");
            dec >>= 1;
        }
    }
    return returnStr;
}

int main()
{
    int a = 1024;
    string str = DecimalToBinary(a);
    cout<<a<<endl;
    cout<<str<<endl;
    return 0;
}

 

 

 

 

 

 

 

EOF

posted on 2012-12-11 15:39  kkmm  阅读(198)  评论(0编辑  收藏  举报