C int转为二进制 再进行与操作

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void toBin(int num,int arr[]);
void andOp(int aBin[],int bBin[]);

main(){
int a;
printf("input first\n");
scanf("%d",&a);

int b;
printf("input se\n");
scanf("%d",&b);

int aBin[8]={0,0,0,0,0,0,0,0};
int bBin[8]={0,0,0,0,0,0,0,0};

toBin(a,aBin);
toBin(b,bBin);

andOp(aBin,bBin);
int i=0;
for(;i<8;i++){
    printf("%d",bBin[i]);
}

}

void toBin(int num,int arr[]){
    int i=7;
    while(num/2!=0){
        arr[i]=num%2;
        //printf("%d",arr[i]);
        i--;
        num=num/2;
    }
    arr[i]=num%2;
    //printf("%d",arr[i]);
}

void andOp(int aBin[],int bBin[]){
    int i=0;
    for(;i<8;i++){
        if((aBin[i]+bBin[i])==2){
            bBin[i]=1;
        }else{
            bBin[i]=0;
        }
    }
    
}

 

posted @ 2013-03-13 14:00  cart55free99  阅读(265)  评论(0编辑  收藏  举报