10 Bitwise operators and Enumeration types 8 bit

10주 강의 Bitwise operators and Enumeration types

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

Bitwise operators Logical operators Shift operators (unary) bitwise complement ~ bitwise and & bitwise exclusive or ^ bitwise inclusive or | left shift << right shift >>
![Operator precedence Operators ( ) [ ] ++(postfix) --(postfix) Associativity left to right ++(prefix) Operator precedence Operators ( ) [ ] ++(postfix) --(postfix) Associativity left to right ++(prefix)](http://slidetodoc.com/presentation_image_h2/f914875c1b00bc3d7a1b2f70b3f2e3f9/image-4.jpg)
Operator precedence Operators ( ) [ ] ++(postfix) --(postfix) Associativity 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 | left to right && left to right || left to right ? : right to left = += -= *= / = %= >>= <<= right to left , (comma operator) &= ^= |= left to right

Bitwise complement • int a=70707; 000000010100 0011 • ~a 111111101011 1100 -70708 (이유는 알겠지!!!!)

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

비교 테이블 Value of n Binary representation Bitwise complement Two’s complement Value representation of -n of –n 7 00000111 11111000 11111001 -7 8 00001000 11110111 11111000 -8 9 00001001 11110110 11110111 -9 -7 11111001 00000110 00000111 7

연산 규칙(boolean) Values of : a b a&b a^b a|b 0 0 0 1 1 0 1 1 1 0 1

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

Left shift Declaration and initialization char c = ‘Z’; Expression Representation Action c 00000000 01011010 unshifted c << 1 00000000 10110100 left-shifted 1 c << 4 00000000101 10100000 left-shifted 4 c << 31 00000000 left-shifted 31

Right shift Declarations and initializations int a = 1 << 31; /*shift 1 to the high bit */ unsigned b = 1 << 31; Expression Representation Action a 100000000 0000 unshifted a >> 3 111100000000 right-shifted 3 b 100000000 0000 unshifted b >> 3 000100000000 right-shifted 3

결합 Declaration and assignments unsigned a = 1, b = 2; Expression Equivalent expression Representation Value a << b >> 1 (a << b) >> 1 00000010 2 a << 1+2 << 3 (a << (1+2)) << 3 0000 01000000 64 a + b << 12 * a >> b ((a + b) << (12 * a)) >> b 00001100 0000 3072

Masks • 00000000 00000001 int i, mask = 1; for (i=0; i<10; ++i) printf(“%d”, i & mask); • 00000000 1111 v & 255 • 0 x 000 f, 0 xf, 017로 masking

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

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

Mask의 예 Expression Binary representation Value p 1111 11001001 01100000 10010111 -3579753 mask 0000 1111 00000000 16711680 p & mask 0000 11001001 00000000 13172736 (p & mask) >> n 00000000 11001001 201

Enumeration Type • • enum day {sun, mon, tue, wed, thu, fri, sat} ; enum day d 1, d 2; if (d 1==d 2) …. 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} veg 1, veg 2; enum {fri, pine} tree; Enum veg {beet, carrot, corn} veg; /* allowed but not good */ 347 page 프로그램 설명

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


- Slides: 20