C 언어: 비트 연산(bitwise operation)_000030
C언어: 매크로사용 000002
/// 연산
변별
/// 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> // bitwise binary octal decimal hex #define A_BASIC 1 //= 000000001 00000001 1 0x00000001 #define A_TYPE1 (A_BASIC << 1) //= 000000010 00000002 2 0x00000002 #define A_TYPE2 (A_BASIC << 2) //= 000000100 00000004 4 0x00000004 // . . . // . . . // . . . unsigned int set_A(unsigned int a); void characteristics_of_A(unsigned int a); int main(void) { unsigned int a; puts("characteristics of A"); puts("--------------------"); puts("A_BASIC : 00000001"); puts("A_TYPE1 : 00000010"); puts("A_TYPE2 : 00000100"); puts("A_BASIC | A_TYPE1 : 00000011"); puts("A_BASIC | A_TYPE2 : 00000101"); puts("A_TYPE1 | A_TYPE2 : 00000110"); puts("A_BASIC | A_TYPE1 | A_TYPE2: 00000111"); puts("--------------------"); puts(""); a = set_A(A_BASIC); characteristics_of_A(a); a = set_A(A_TYPE1); characteristics_of_A(a); a = set_A(A_TYPE2); characteristics_of_A(a); a = set_A(A_BASIC | A_TYPE1); characteristics_of_A(a); a = set_A(A_BASIC | A_TYPE2); characteristics_of_A(a); a = set_A(A_TYPE1 | A_TYPE2); characteristics_of_A(a); a = set_A(A_BASIC | A_TYPE1 | A_TYPE2); characteristics_of_A(a); return 0; } unsigned int set_A(unsigned int a) { return a; } void characteristics_of_A(unsigned int a) { switch(a) { case A_BASIC: puts("A uses A_BASIC"); /* << */ break; case A_TYPE1: puts("A uses A_TYPE1"); /* << */ break; case A_TYPE2: puts("A uses A_TYPE2"); /* << */ break; case A_BASIC | A_TYPE1: puts("A uses A_BASIC | A_TYPE1"); /* << */ break; case A_BASIC | A_TYPE2: puts("A uses A_BASIC | A_TYPE2"); /* << */ break; case A_TYPE1 | A_TYPE2: puts("A uses A_TYPE1 | A_TYPE2"); /* << */ break; case A_BASIC | A_TYPE1 | A_TYPE2: puts("A uses A_BASIC | A_TYPE1 | A_TYPE2"); /* << */ break; default: puts("ERROR!"); /* << */ break; } } | cs |
* 실행환경: Linux(5.7.6-x86_64-linode136)
* 컴파일러: gcc (Ubuntu 6.5.0-2ubuntu1~14.04.1) 6.5.0 20181026
- 당신을 응원합니다. -
댓글
댓글 쓰기