/* Converts the unsigned integer k to binary character form with a blank
after every fourth digit. Result is in string s of length 39. Caution:
If you want to save the string, you must move it. This is intended for
use with printf, and you can have only one reference to this in each
printf statement. */
char * binary(unsigned k) {
int i, j;
static char s[40] = "0000 0000 0000 0000 0000 0000 0000 0000";
j = 38;
for (i = 31; i >= 0; i--) {
if (k & 1) s[j] = '1';
else s[j] = '0';
j = j - 1;
k = k >> 1;
if ((i & 3) == 0) j = j - 1;
}
return s;
}
我在C++代码黑客喜悦
#include <iostream>
using namespace std;
char *binary(unsigned k){
int i, j;
static char s[40]="0000 0000 0000 0000 0000 0000 0000 0000";
j=38;
for (i=31;i>=0;i--){
if (k & 1) s[j]='1';
else s[j]='0';
j=j-1;
k=k>>1;
if ((i & 3)==0) j=j-1;
}
return s;
}
int main(){
unsigned k;
cin>>k;
*binary(k);
return 0;
}
测试,但不ķ什么价值呢?例如我已经输入127,但它返回0为什么?