看《编程之美》第二节的时候,它是定义的一个整型,然后取位。但是他的那个或运算符号好像写错了,写成了异或符号“^”,应该是“|”。我就突然对二进制的输出感兴趣了。想知道怎样输出二进制。我们知道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) return;
else BinaryRecursion(n);
cout〈〈a;
}
//使用容器转换二进制
void BinaryVector(int n)
{
int temp;
temp=n;
list〈int〉 L;
while(temp!=0)
{
L.push_front(temp%2);
temp=temp〉〉1;
}
for (list〈int〉::iterator iter=L.begin();iter!=L.end();iter+