"由键盘输入一个小于256的十进制正整数,然后将该数转换成八位的二进制数形式输出。"的c++程序

望得到编程高手的相助 ,非常感谢啊!
2024-11-09 21:38:08
推荐回答(4个)
回答1:

#include "stdio.h"
int main()
{
int i;
int j = 128;
scanf("%d", &i);
while(j > 1)
{
if(i/j == 1)
{
printf("1");
}
else
printf("0");
i = i % j;
j = j/2;
}
printf("%d", i);

}

回答2:

#include

void tobin(char x)
{
int i;
char bin[8];
for(i = 7; i >= 0; i--)
{
bin[i] = x&1;
x = x>>1;
}
for(i = 0; i < 8; i++)
{
printf("%d", bin[i]);
}
printf("\n");
}

int main()
{
unsigned char a = 254;
tobin(a);
return 0;
}

回答3:

main()
{
unsigned char a;
scanf("%d",&a);
printf("%o", a);
}

回答4:

#include
using namespace std;
int main()
{
int n;
cout << "input n:";
cin >> n;
for(int i=1; i<=8; i++){
cout << (n<0?1:0) << (i%8==0?" ":"");
n = n<<1;
}
cout << endl;
}