Computer/초보자를 위한 C 언어 300제
029. 비트 연산자 이해하기(|, &, ~, ^, <<, >>)
Theo Kim
2010. 11. 12. 03:18
#include <stdio.h> main() { unsigned char ch = 255, mask = 0x7F; printf( "%d \n",ch ); // 255 printf( "%d \n",ch & mask); // 127 printf( "%d \n",(char)~ch ); // 0 printf( "%d \n",ch ^ ch ); // 0 printf( "%d \n",ch >> 1 ); // 127 printf( "%d \n",mask << 1 ); // 254 }
관심있게 볼 부분은 mask이다. 이것은 IP Address와 Subnet Mask를 비트 마스크할 때와 같은 원리이다.
이러한 비트 연산자를 통해 빠른 연산을 할 수 있다.
물론 우리의 Desktop은 해당 사항은 별로 없을 것 같다.
아마 특수 형태 H/W, 예를 들면 방화벽에서는 빠른 연산이 필수 일 것이다.