想知道怎样输出二进制。我们知道C++输出十六进制是cout〈〈hex〈〈 a;而八进制是cout〈〈 ocx〈〈 a;二进制则没有默认的输出格式,需要自己写函数进行转换,于是上网搜索了一下。网上思路真是广泛啊。下面列出一些方法。 #include 〈iostream〉 #include 〈list〉 #include 〈bitset〉 using namespace std; //递归输出二进制函数 - void BinaryRecursion(int n)
- {
- int a;
- a=n%2;
- n=n〉〉1;
- if (n==0)
- ;
- else
- BinaryRecursion(n);
- cout〈〈a;
- }
复制代码
|