C++中怎样把整数输出为二进制数?该怎么解决
C++中怎样把整数输出为二进制数?
C++中怎样把整数输出为二进制数?
#include <iostream.h>
void main()
{
int a = 42486;
cout <<oct <<a <<endl
<<hex <<a <<endl;
unsigned b = 42486;
cout << dec <<(signed)b <<endl;
}
------解决方案--------------------
用bitset吧
------解决方案--------------------
#include <iostream>
#include<bitset>
using namespace std;
int main()
{
int a=42486;
bitset<32> bs(a);
cout<<bs<<endl;
system("pause");
return 0;
}
------解决方案--------------------
C++中怎样把整数输出为二进制数?
#include <iostream.h>
void main()
{
int a = 42486;
cout <<oct <<a <<endl
<<hex <<a <<endl;
unsigned b = 42486;
cout << dec <<(signed)b <<endl;
}
------解决方案--------------------
用bitset吧
------解决方案--------------------
#include <iostream>
#include<bitset>
using namespace std;
int main()
{
int a=42486;
bitset<32> bs(a);
cout<<bs<<endl;
system("pause");
return 0;
}
------解决方案--------------------
- C/C++ code
#include <iostream> using namespace std; int main() { int num = 16; while (num) { cout<< (num & 1); num >>= 1; } cout<< endl; return 0; }
------解决方案--------------------
printf("%x",a);
------解决方案--------------------
2楼的正解,我说一个不需要使用STL的方法。
- C/C++ code
#include <iostream> using namespace std; int main() { int n, i, j; bool arr[100]; while(cin >> n) { i = 0; while(n > 0) { arr[i] = bool(n % 2); i++; n /= 2; } for(j=i-1;j>=0;j--) cout << arr[j]; cout << endl; } return 0; }