1. 목적
 Boolean 연산자와 논리 연산자 이해

2. 문제
 AND, XOR, OR 연산 진리표 작성 프로그램하라.
 a 와 b 는 각각 피연산자이며, & 는 AND, ^ 는 XOR, | 는 OR 연산자를 의미한다.
 따라서 출력 결과는 아래와 같다.

┌───┬───┬───┬───┬───┐
│    a   │   b    │  a&b │ a^b │  a|b  │
├───┼───┼───┼───┼───┤
│    0   │    0   │   0    │    0   │    0   │
│    0   │    1   │   0    │    1   │    1   │
│    1   │    0   │   0    │    1   │    1   │
│    1   │    1   │   1    │    0   │    1   │
└───┴───┴───┴───┴───┘


3. 이해

 - 논리연산 AND, XOR, OR 이해 및 C 로 구현
 - printf 문 기초

4. 풀이

#include <stdio.h>

#define false 0
#define true 1


int main()
{
 
 printf("┌───┬───┬───┬───┬───┐\n");
 printf("│  a   │  b   │ a&b  │ a^b  │ a|b  │\n");
 printf("├───┼───┼───┼───┼───┤\n");
 printf("│  %d   │  %d   │  %d   │  %d   │  %d   │\n", false, false, false & false, false & false, false ^ false, false | false);
 printf("│  %d   │  %d   │  %d   │  %d   │  %d   │\n", false, true, false & true, false & true, false ^ true, false | true);
 printf("│  %d   │  %d   │  %d   │  %d   │  %d   │\n", true, false, true & false, true & false, true ^ false, true | false);
 printf("│  %d   │  %d   │  %d   │  %d   │  %d   │\n", true, true, true & true, true & true, true ^ true, true | true);
 printf("└───┴───┴───┴───┴───┘\n");
 return 0;
}

5. 코드해석

  간단하게 boolean 형 연산자를 정의해서 썼습니다. AND, OR, XOR 는 문제에서 주어진 연산자와 동일한 부호를 쓰므로 표현에 어려움이 없었습니다. 주의해야할 것은 이 코드에서는 boolean 연산자를 만들어 줘야합니다. C++ 에서는 문제가 없었습니다. 만드실때 확장자를 주의해서 봐 주시기 바랍니다. 컴파일러가 인식을 C 로 하나 C++ 로 하나에 따라 결과다 다르게 나올때가 있습니다.

6. 추가정보
  Boolean 형 자료 : http://mwultong.blogspot.com/2006/10/c-bool-boolean.html
  논리 연산자 : http://myhome.hanafos.com/~kukdas/doc/c/c-10.html
  printf 함수 : http://racy.egloos.com/571919

Posted by 카켈