if (4 && 3) printf("hura!\n"); if (4 & 3) printf("hura hura\n"); 0 = false nenulove = true 4 && 3 => 1 100 & 011 ===== 000 = 0 ---------------------------------- printf("%d\n", 4 || 3); // vypise 1 printf("%d\n", 4 | 3); // vypise 7 100 | 011 ====== 111 => 7 !4 => 0 ~4 => ~(00000100) => 11111011 = 251 11111111 => 255 100000000 x + ~x = 255 2^N 1 * 2 * 2 * 2 * 2 2 * 1010111011 => 10101110110 10010101100 / 2 => 1001010110 ================================ 0 & 0 => 0 1 & 0 => 0 0 & 1 => 0 1 & 1 => 1 x AND 0 => vynulujem bit x AND 1 => zachovam bit 0010101101101 & 0000111011100 ================= 0000101001100 ================================== 0 | 0 => 0 1 | 0 => 1 0 | 1 => 1 1 | 1 => 1 x OR 0 => zachova x OR 1 => nastavi na 1 011011101011011 | 100110101111100 ==================== 111111101111111 ================================= A B A XOR B ================ 0 0 0 0 1 1 1 0 1 1 1 0 17 XOR 42 => 59 59 XOR 42 => 17 010001 101010 ======== 111011 => 59 0 XOR 0 => 0 1 XOR 0 => 1 0 XOR 1 => 1 1 XOR 1 => 0 cvicenie: mam x a chcem sa dozvediet hodnotu 3. bitu, pricom vysledok chcem v podobe 0 alebo 1. x = 0101111110 & 1000 ================= 0000001000 (x & (1 << 3)) >> 3 (x >> 3) & 1 1 << 0 = 1 1 << 1 = 2 1 << 2 = 4 1 << 3 = 8 chytim 1 a posuniem ju 3-krat vlavo vysledok zadnujem s x vysledok posuniem 3-krat vpravo mam x a chcem sa dozvediet hodnotu i. bitu, pricom vysledok chcem v podobe 0 alebo 1. (x >> i) & 1 ============= x = 0101110101 y = x >> 1 z = z << 1 ============= dalsie cvicenie: mam x a chcem ziskat rovnaku hodnotu ale taku, kde na i. bite je 1: x | (1 << i) mam x a chcem ziskat rovnaku hodnotu ale taku, kde na i. bite je 0: x & ~(1 << i) 11111111101111