CHAPTER 7 ========= bitwise operators ----------------- o act on integral expressions o precedence: ~ ; <<,>> ; & ; ^ ; | 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 Example: int a=1, b=2; (a+b << 12*a >> b) is equivalent to (((a+b)<<(12*a))>>b) and yields 00001100 00000000 ==================================================================== Operator precedence and associativity, a final look --------------------------------------------------- Operators Associativity ------------------------------------------------------------- () [] . -> ++(postfix) --(postfix) left to right ------------------------------------------------------------- +(unary) -(unary) ++(prefix) --(prefix) ! right to left sizeof(type) &(address) *(dereference) ~ ------------------------------------------------------------- * / % 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 ------------------------------------------------------------- = += -= *= /= &= >>= etc right to left ------------------------------------------------------------- ,(comma operator) left to right ------------------------------------------------------------- ==================================== (int-to-binary.c) /* Bit print an int expression */ #include #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(' '); } } 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 = ((1<> n); }