본문 바로가기

C

[C언어] 비트 연산 (Letter Attribute)

반응형
#include<stdio.h>

#define BOLD   (0x01 << 0)   // 0x01 // 1
#define SHADOW (0x01 << 1)   // 0x02 // 2
#define ITALIC (0x01 << 2)   // 0x04 // 4
#define UL     (0x01 << 3)   // 0x08 // 8

int main(){

   unsigned char attribute;

   attribute = attribute ^ attribute;         // ^(xor) : 두 비트가 서로 다르면 1을 반환한다.
   printf("attribute : %02x\n", attribute);   // 0000 0000, %x는 16진수를 출력한다.

   attribute = attribute | BOLD;              // |(or) : 두 비트 중 하나라도 1이라면 1을 반환한다.
   printf("attribute : %02x\n", attribute);   // 0000 0001

   attribute = attribute | (SHADOW + ITALIC);
   printf("attribute : %02x\n", attribute);   // 0000 0111

   attribute = attribute & (~BOLD);   // &(and) : 두 비트가 1이면 1을 반환한다.
                                      // ~(not) : 비트를 반전 시킨다. (0->1, 1->0)
   printf("attribute : %02x\n", attribute);   // 0000 0110

   return 0;
}

 

반응형