Presupunem că avem vectorul unsigned char s[3]; . s[0], s[1], s[2] fiecare reprezintă câte o variabilă pe 8 biți pe care initial ii setam pe 0.
s[0] | s[1] | s[2] | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$b_7$ | $b_ 6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | |||||
biti din S | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
În plus facem următoarea asociere:
s[0] | s[1] | s[2] | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$b_7$ | $b_ 6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | |||||
biti din S | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||
numere asociate | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
Explicație:
Observație! card_set(s) == 0 (0 elemente in s) și is_empty_set(s) == 1 (este adevărat că s e gol).
Să presupunem că pe mulțimea s de mai sus (inițial goală) se execută operația următoarea:
insert_in_set(s, 8);
Noua mulțime s este s = {8} și arată astfel:
s[0] | s[1] | s[2] | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$b_7$ | $b_ 6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | |||||
biti din S | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||
numere asociate | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
Explicație: numărul 8 a fost asociat cu bitul 0 ( 8 % 8 ) in byte-ul 1 (8 / 8). Acest bit a fost facut 1.
Observație! card_set(s) == 1 (1 element in s) și is_empty_set(s) == 0 (este false că s e gol).
Să presupunem că pe mulțimea s curentă se execută operația următoarea:
insert_in_set(s, 19);
Noua mulțime s este s = {8, 19} și arată astfel:
s[0] | s[1] | s[2] | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$b_7$ | $b_ 6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | |||||
biti din S | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | ||||
numere asociate | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
Explicație: numărul 19 a fost asociat cu bitul 3 ( 19 % 8 ) in byte-ul 2 (19 / 8). Acest bit a fost facut 1.
Observație! card_set(s) == 2 (2 elemente in s) și is_empty_set(s) == 0 (este false că s e gol).
Să presupunem că pe mulțimea s curentă se execută următoarele 2 operații:
printf("%d", is_in_set(s, 3)); // afișează 0 printf("%d", is_in_set(s, 19)); // afișează 1
Să presupunem că pe mulțimea s curentă se execută operația următoarea:
delete_from_set(s, 19);
Noua mulțime s este s = {8} și arată astfel:
s[0] | s[1] | s[2] | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$b_7$ | $b_ 6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | $b_7$ | $b_6$ | $b_5$ | $b_4$ | $b_3$ | $b_2$ | $b_1$ | $b_0$ | |||||
biti din S | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||
numere asociate | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
Explicație: numărul 19 a fost asociat cu bitul 3 ( 19 % 8 ) in byte-ul 2 (19 / 8). Acest bit a fost facut 0.
Observație! card_set(s) == 1 (1 element in s) și is_empty_set(s) == 0 (este false că s e gol).
Să presupunem că pe mulțimea s curentă se execută următoarele 2 operații:
printf("%d", is_in_set(s, 19)); // afișează 0