CHAPTER 7 ========= bitwise operators ----------------- o act on integral epxressions o system dependent o we assume here 8-bit bytes, 4-byte words, ascii character code example ------- int a = 33333, b = -77777; Expression Representation Value a 00000000 00000000 10000010 00110101 33333 b 11111111 11111110 11010000 00101111 -77777 a & b 00000000 00000000 10000000 00100101 32805 a ^ b 11111111 11111110 01010010 00011010 -110054 a | b 11111111 11111110 11010010 00111111 -77249 ~(a | b) 00000000 00000001 00101101 11000000 77248 ~a & ~b 00000000 00000001 00101101 11000000 77248 =================================================================== left and right shift -------------------- o integral promotion of each operand o the type of the result is the promoted type of the left-hand side operand example ------- char c = 'Z'; int a = 1 << 31; /* shift 1 to the high bit */ unsigned b = 1 << 31; Expression Representation Action c 00000000 00000000 00000000 01011010 unshifted c << 4 00000000 00000000 00000101 10100000 left-shifted 4 a 10000000 00000000 00000000 00000000 unshifted a >> 3 11110000 00000000 00000000 00000000 right-shifted 3 b 10000000 00000000 00000000 00000000 unshifted b >> 3 00010000 00000000 00000000 00000000 right-shifted 3 ==================================================================== /* Bit print an int expression */ #include void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* in limits.h */ int mask = 1 << (n - 1); /* mask = 100...0 */ for (i = 1; i <= n; ++i) { putchar(((a & mask) == 0) ? '0' : '1'); a <<= 1; if (i % CHAR_BIT == 0 && i < n) putchar(' '); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* Test bit print */ include void bit_print(int a); int main(void) { int k; for ( ; ; ) { printf("Input an integer: "); scanf("%d", &k); printf("\n%7d = ", k); bit_print(k); printf("\n\n"); } return 0; } ====================================================================== /* Pack 4 characters into an int */ #include int pack(char a, char b, char c, char d) { int p = a; /* p will be packed with a, b, c, d */ p = (p << CHAR_BIT) | b; p = (p << CHAR_BIT) | c; p = (p << CHAR_BIT) | d; return p; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* Unpack a byte from an int */ #include char unpack(int p, int k) /* k = 0, 1, 2, or 3 */ { int n = k * CHAR_BIT; /* n = 0, 8, 16, or 24 */ unsigned mask = 255; /* low-order byte */ mask <<= n; return ((p & mask) >> n); } ====================================================================== /* Create employee data in a short int. */ short create_employee_data(int id_no,int job_type,char gender) { short employee = 0; /* start with all bits off */ employee |= (gender == 'm' || gender == 'M') ? 0 : 1; employee |= job_type << 1; employee |= id_no << 7; return employee; } ====================================================================== enumeration types ----------------- enum - keyword type declaration: enum day {sun, mon, tue, wed, thu, fri, sat}; sun, mon, ... are int constants: sun==0, mon==1, ... 'enum day' is a type, declaring variables: enum day d1, d2; d1 = fri; if (d1 == d2) ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~ enumerators can be intialized ----------------------------- enum suit {clubs = 1, diamonds, hearts, spades} a, b, c; enum fruit {apple = 7, pear, orange = 3, lemon} frt; ~~~~~~~~~~~~~~~~~~~~~~~~~~~ OK if tag name is missing, enum {fir, pine} tree; but cannot define more variables of the type: defining a shorter type name: enum day {sun, mon, tue, wed, thu, fri, sat}; typdef enum day day;