2009-09-23

Post date: Sep 23, 2009 10:39:01 PM

    • Review
      • Bitwise operators & | ~ treat an integer as a vector of bits.
      • Logical (boolean) operators && || ! treat non-zero integer as true, and zero integer as false.
    • Practice problem 2.13, use gdb commands as a simple command line calculator.
      • set var $x = 0x66 /* sets gdb pseudo variable $x to value 0x66 */
      • set var $y = 0x93 /* sets gdb pseudo variable $y to value 0x93 */
      • p /x $x & $y /* prints the result of 0x66 & 0x93 in hexadecimal */
      • ....
    • Practice problem 2.14, write x == y without using == operator (see eq.c attached).
      • If we see x and y as bit sets, x & ~y is the set difference x - y, and y & ~x is the set difference y - x. If x ⊆ y, then x - y = ∅. Likewise, if y ⊆ x, then y - x = ∅. If x ⊆ y and y ⊆ x, then x = y. So the expression !(x & ~y) && !(y & ~x) works.
      • Using XOR, x ^ x = 0 and x ^ y != 0 if x != y. So the expression !(x ^ y) also works.
    • Practice problem 2.23, pass gcc flags -Wall -Wextra to tell gcc to report many common mistakes. In particular, the warning for this particular problem is -Wsign-compare.
    • Big endian vs. little endian machines (see endian.c attached).
      • int i = 0x44332211, on little endian the bytes are stored in memory as 11 22 33 44. On big endian, the bytes are stored as 44 33 22 11.
      • On both 32-bit and 64-bit machines, sizeof(int) is 4 bytes (32-bits). However, sizeof(long int) is 4 bytes (32-bits) on 32-bit machine and 8 bytes (64-bits) on 64-bit machine.
      • On little endian 64-bit machine, long int i = 0x44332211 is stored in memory as 11 22 33 44 00 00 00 00.