Presentation is loading. Please wait.

Presentation is loading. Please wait.

10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.

Similar presentations


Presentation on theme: "10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes."— Presentation transcript:

1 10 주 강의 Bitwise operators and Enumeration types

2 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes

3 Bitwise operators Logical operators(unary) bitwise complement~ bitwise and& bitwise exclusive or^ bitwise inclusive or| Shift operatorsleft shift<< right shift>> Bitwise operators

4 OperatorsAssociativity ( ) [ ] ++(postfix) --(postfix)left to right ++(prefix) --(prefix) ! ~ size of(type) +(unary) -(unary) &(address) * (dereference) right to left * / %left to right + -left to right >left to right >=left to right == !=left to right & ^ | &&left to right ||left to right ?:right to left = += -= *= / = %= >>= <<= &= ^= |= right to left, (comma operator)left to right Operator precedence

5 Bitwise complement int a=70707; 00000000 00000001 00010100 00110011 ~a 11111111 11111110 11101011 11001100  -70708 ( 이유는 알겠지 !!!!)

6 Two ’ s complement 설명했으니 추가 설명은 안 함.. 예 7 : 00000000 00000111 -7 : 11111111 11111001

7 비교 테이블 Value of n Binary representationBitwise complement Two ’ s complement representation of -n Value of – n 700000000 0000011111111111 1111100011111111 11111001-7 800000000 0000100011111111 1111011111111111 11111000-8 900000000 0000100111111111 1111011011111111 11110111-9 -711111111 1111100100000000 0000011000000000 000001117

8 Values of : aba&ba^ba|b 00000 10011 01011 11101 연산 규칙 (boolean)

9 Bitwise binary logical operators Declaration and initializations int a = 33333, b = -77777; ExpressionRepresentationValue a00000000 00000000 10000010 00110101 33333 b11111111 11111110 11010000 00101111-77777 a & b00000000 00000000 10000000 00100101 32805 a ^ b11111111 11111110 01010010 00011010-110054 a | b11111111 11111110 11010010 00111111-77249 ~(a | b)00000000 00000001 00101101 11000000 77248 (~a & ~b)00000000 00000001 00101101 11000000 77248

10 Left shift Declaration and initialization char c = ‘ Z ’ ; ExpressionRepresentationAction c00000000 00000000 00000000 01011010unshifted c << 100000000 00000000 00000000 10110100left-shifted 1 c << 400000000 00000000 00000101 10100000left-shifted 4 c << 3100000000 00000000 left-shifted 31

11 Declarations and initializations int a = 1 << 31; /*shift 1 to the high bit */ unsigned b = 1 << 31; Expression Representation Action a10000000 00000000 00000000 00000000unshifted a >> 311110000 00000000 00000000 00000000right-shifted 3 b10000000 00000000 00000000 00000000unshifted b >> 300010000 00000000 00000000 00000000right-shifted 3 Right shift

12 Declaration and assignments unsigned a = 1, b = 2; ExpressionEquivalent expressionRepresentationValue a > 1(a > 1 00000000 00000010 2 a << 1+2 << 3(a << (1+2)) << 3 00000000 01000000 64 a + b > b((a + b) > b 00001100 00000000 3072 결합

13 Masks 00000000 00000000 00000000 00000001 int i, mask = 1; for (i=0; i<10;++i) printf( “ %d ”, i & mask); 00000000 00000000 00000000 11111111 v & 255 0x000f, 0xf, 017 로 masking

14 Printing an int bitwise 338page 프로그램 설명 Idea: 왼쪽으로 이동하면서 most significant bit 를 출력 int n = sizeof(int) * CHAR_BIT int n = 1 << (n-1);

15 Packing and Unpacking 4 characters  1 word 341page 프로그램 설명 (pack) 342page 프로그램 설명 (unpack)

16 Mask 의 예 ExpressionBinary representationValue p11111111 11001001 01100000 10010111-3579753 mask00000000 11111111 00000000 0000000016711680 p & mask00000000 11001001 00000000 0000000013172736 (p & mask) >> n 00000000 00000000 00000000 11001001 201

17 Enumeration Type enum day {sun, mon, tue, wed, thu, fri, sat} ; enum day d1, d2; if (d1==d2) …. enum suit {clubs =1, diamonds, hearts, spades} a,b,c ; /* clubs  1,.. spades  3 */ enum fruit {apple=7, pear, orange=3, lemon} frt; enum veg {beet=17, carrot =17, corn=17} veg1, veg2; enum {fri, pine} tree; Enum veg {beet, carrot, corn} veg; /* allowed but not good */ 347page 프로그램 설명

18 쉬운 연산 방법 enum day {sun, mon, tue, wed, fri, sat}; typedef enum day day; day find_next_day(day d) { day next_day; return ((day) ((int) d + 1) % 7));

19 가위, 바위, 보 경기 (The Game of Paper, Rock, Scissors ) 프로그램을 검증 후 이해할 것

20 숙제 학교 : 4, 5, 16, 26, 27 집 : 2, 3, 6, 10, 15, 20


Download ppt "10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes."

Similar presentations


Ads by Google