C logical operators: operands are any type, non-zero is true, result is 0 or 1: ! NOT && AND || OR C bitwise operators: operands must be some integer type: ~ NOT << shift left >> shift right & AND ^ XOR | OR <<= >>= &= ^= |= assignments Examples: 5 5 = 0101 && 2 & 2 = 0010 ---- ---------- 1 0 = 0000 If a signed integer is shifted right, the sign bit may or may not be extended, so don't count on it, e.g. (0b10...0 >> 1) may produce 0b110...0 or 0b010...0 (0b10101... is Python, not C. In C or Python: 012 == 10, 0x12 == 18) Given an unsigned integer x: x & 1 look at bit 0, result is 0 or 1 x & (1 << k) look at bit k, result is 0 or (1 << k) (x >> k) & 1 look at bit k, result is 0 or 1 x & 3 look at least significant 2 bits, result is 0, 1, 2, or 3 x |= (1 << k) set bit k high, (1 << k) == 0b0...010...0 x &= ~(1 << k) set bit k low, ~(1 << k) == 0b1...101...1 x |= 3 set least significant 2 bits high, 3 == 0b0...011 x &= ~3 set least significant 2 bits low, ~3 == 0b1...100 x ^= (1 << k) flip bit k // r = count of number of 1's in x // r = 0; while( x) { r += (x & 1); x >>= 1; } // r = bits of x in reverse order, n is bit-length of x // r = 0; for( i = 0; i < n; ++i) { r <<= 1; r |= x & 1; x >>= 1; }