把一个正整数转为32位二进制字符串,其中数据范围是 0 < = x < 2 31 0<=x<2^{31} 0<=x<231
#include<iostream>
using namespace std;
void itobs(int i, char* c)
{
int j = 0;
while (j < 32)
{
if ((i & (1 << (31 - j))) != 0) c[j] = '1';
else c[j] = '0';
j++;
}
c[32] = 0; //字符串结束标志
}
int main()
{
char c[33];
itobs(1, c);
cout << c;
return 0;
}