我正在尝试输出 0-130 的位模式,但不知何故,我只是在相应的位模式应该在哪里得到颠倒的问号。代码的外观如下。有什么建议么?
include <iostream>
using namespace std;
int isSet( unsigned int x, int i );
string bitsetfn(unsigned x, int nbits);
int main() {
for (int counter = 0; counter <=130; counter++){
cout << counter << " is: " << bitsetfn(counter,16)<<endl;
}
return 0;
}
string bitsetfn(unsigned x, int nbits){
string s=" ";
for(int j=0; j<nbits; j++) {
s+=isSet(x,j);
}
return s;
}
int isSet( unsigned int x, int i ) {
return ((x >> i) & 1);
}
这就是输出应该是这样的......
0 is: 0000000000000000
1 is: 0000000000000001
2 is: 0000000000000010
3 is: 0000000000000011
4 is: 0000000000000100
5 is: 0000000000000101
6 is: 0000000000000110
7 is: 0000000000000111
8 is: 0000000000001000
9 is: 0000000000001001
10 is:0000000000001010
11 is: 0000000000001011