输出一个整数的每一位
方法一:逆序输出
#include<stdio.h>
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
while(data>0)
{
if(data%2==1)
{
printf("1");
}
else{
printf("0");
}
data/=2;
}
system("pause");
return 0;
}
方法一:逆序输出
#include<stdio.h>
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
while(data>0)
{
if(data%2==1)
{
printf("1");
}
else{
printf("0");
}
data/=2;
}
system("pause");
return 0;
}
方法二:正序输出
#include<stdio.h>
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
int arr[32];
int i = 0;
for(;i<32;i++)
{
arr[i] = 0;
}
i=0;
while(data>0)
{
if(data%2==1)
{
arr[i] = 1;
}
else
{
arr[i] = 0;
}
i++;
data/=2;
}
for(i=31;i>=0;i--)
{
printf("%d",arr[i]);
}
printf("\n");
system("pause");
return 0;
}
#include<stdio.h>
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
int arr[32];
int i = 0;
for(;i<32;i++)
{
arr[i] = 0;
}
i=0;
while(data>0)
{
if(data%2==1)
{
arr[i] = 1;
}
else
{
arr[i] = 0;
}
i++;
data/=2;
}
for(i=31;i>=0;i--)
{
printf("%d",arr[i]);
}
printf("\n");
system("pause");
return 0;
}
方法三:移位方法
#include<stdio.h>
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
int pos = 31;
while (pos >= 0){
if (data & (1 << pos)){
printf("1 ");
}
else{
printf("0 ");
}
pos--;
}
printf("\n");
system("pause");
return 0;
}
#include<windows.h>
int main()
{
printf("Please Enter:");
int data = 0;
scanf("%d",&data);
int pos = 31;
while (pos >= 0){
if (data & (1 << pos)){
printf("1 ");
}
else{
printf("0 ");
}
pos--;
}
printf("\n");
system("pause");
return 0;
}