bit연산 프로그램으로 and, or, xor, comp의 연산을 가능하게 작성하였다

#include <stdio.h>

 

int MENU(); //함수 선언

void AND();

void OR();

void XOR();

void Complement();

 

int main(void)

{         

           int ex; //종료를 위한 변수

          

           do {

                     ex = MENU();

           } while (ex != -1);

          

           return 0;

}

/****************************/

int MENU() //메뉴함수

{

           int me; //메뉴

 

           printf("\n*******************비트연산프로그램*******************\n");

           printf("1.AND\t2.OR\t3.XOR\n4.Complement(~)\t5.종료\n");

           printf("******************************************************\n");

           printf("\n메뉴선택 : ");

           scanf("%d",&me);

          

           switch(me) {

           case 1:

                     AND();

                     break;

           case 2:

                     OR();

                     break;

           case 3:

                     XOR();

                     break;

           case 4:

                     Complement();

                     break;

           case 5:

                     return -1;

                     break;

           default :

                     printf("잘못된 입력입니다.\n");

           }

          

           return 0;

}

/****************************/

void AND() { //AND함수

           int a,b,i,mask=1;

           int temp;

 

           printf("두 정수를 입력하시오 : ");

           scanf("%d %d",&a,&b);

 

           printf("   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(a&mask<<i)?1:0);

           printf("\nAND\t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(b&mask<<i)?1:0);

           temp = a & b;

           printf("\n--------------------------------------------------------------");

           printf("\n   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(temp&mask<<i)?1:0);

           printf(" = %d\n",temp);

}

/****************************/

void OR() { //OR함수

           int a,b,i,mask=1;

           int temp;

 

           printf("두 정수를 입력하시오 : ");

           scanf("%d %d",&a,&b);

 

           printf("   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(a&mask<<i)?1:0);

           printf("\nOR \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(b&mask<<i)?1:0);

           temp = a | b;

           printf("\n--------------------------------------------------------------");

           printf("\n   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(temp&mask<<i)?1:0);

           printf(" = %d\n",temp);

}

/****************************/

void XOR() { //XOR함수

           int a,b,i,mask=1;

           int temp;

 

           printf("두 정수를 입력하시오 : ");

           scanf("%d %d",&a,&b);

 

           printf("   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(a&mask<<i)?1:0);

           printf("\nXOR\t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(b&mask<<i)?1:0);

           temp = a ^ b;

           printf("\n--------------------------------------------------------------");

           printf("\n   \t\t");

           for(i=31;i>=0;i--)

                     printf("%d",(temp&mask<<i)?1:0);

           printf(" = %d\n",temp);

}

/****************************/

void Complement() { //Complement함수

           int a,i,mask=1;

           int temp;

 

           printf("하나의 정수를 입력하시오 : ");

           scanf("%d",&a);

 

           printf("\nCOMPLEMENT\t");

           for(i=31;i>=0;i--)

                     printf("%d",(a&mask<<i)?1:0);

           temp = ~a;

           printf("\n--------------------------------------------------------------");

           printf("\n          \t");

           for(i=31;i>=0;i--)

                     printf("%d",(temp&mask<<i)?1:0);

           printf(" = %d\n",temp);

}

+ Recent posts