Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 6
- void set_clear(int*);
- void toggle_on(int*, int);
- //void toggle_off(int*, int);
- void differentiate (int*, int*);
- int *intersect(int*, int*);
- int *unify(int*, int*);
- int compare_test(int*, int*, int);
- int visit_bit(int*, int);
- void print_array(int*);
- int main()
- {
- //int difference[SIZE], intersection[SIZE], unity[SIZE];
- int X1[SIZE] = {0, 12, 56, 32, 34, 32};
- int X2[SIZE] = {5, 18, 55, 21, 30, 32};
- //voglio assicurarmi che siano tutti e 3 puliti
- //set_clear(difference);
- //set_clear(intersection);
- //set_clear(unity);
- differentiate(X1, X2);
- //intersection[SIZE] = intersect(X1, X2);
- //unity[SIZE] = unify(X1, X2);
- print_array(X1);
- print_array(X2);
- //print_array(difference);
- //print_array(intersection);
- //print_array(unity);
- }
- void set_clear(int bitArray[SIZE])
- {
- int i;
- for(i = 0; i < SIZE * 32; i++)
- {
- bitArray[i] = 0;
- }
- }
- void toggle_on(int bitArray[SIZE], int k)
- {
- int i = k/32;
- int pos = k%32;
- unsigned int flag = 1;
- flag = flag << pos;
- bitArray[i] = bitArray[i] | flag;
- //una versione più breve sarebbe stata A[k/32] |= 1 << (k%32);
- }
- /*void toggle_off(int bitArray[SIZE], int k)
- {
- int i = k/32;
- int pos = k%32;
- unsigned int flag = 1;
- flag = flag << pos;
- flag = ~flag;
- bitArray[i] = bitArray[i] & flag;
- //allo stesso modo la versione breve ha & invece di | e not (~)
- }*/
- //non è il modo più elegante di fare un test
- int compare_test(int X1[SIZE], int X2[SIZE], int k)
- {
- int i = k/32;
- int pos = k%32;
- unsigned int flag = 1;
- flag = flag << pos;
- if((X1[i] & flag) == 1)
- {
- if((X2[i] & flag) == 1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- if((X2[i] & flag) == 1)
- {
- return 2;
- }
- else return -1;
- }
- }
- void differentiate(int X1[SIZE], int X2[SIZE])
- {
- print_array(X1);
- int k = 0;
- int i = k/32;
- int pos = k%32;
- unsigned int flag = 1;
- flag = flag << pos;
- while(k < 32 * SIZE)
- {
- if((X1[i] & flag) && (X2[i] & flag) == 0)
- {
- printf("1, ");
- k++;
- }
- else
- {
- printf("0, ");
- k++;
- }
- }
- printf("\n\n");
- }
- /*void differentiate(int X1[SIZE], int X2[SIZE])
- {
- int counter = 0;
- while(counter < 32 * SIZE)
- {
- int c = compare_test(X1, X2, counter);
- printf("%d", c);
- if(c == 0)
- {
- printf("1, ");
- counter++;
- }
- else
- {
- printf("0, ");
- counter++;
- }
- }
- printf("\n\n");
- }*/
- int *intersect(int X1[SIZE], int X2[SIZE])
- {
- int X3[SIZE];
- int counter = 0;
- while(counter < 32 * SIZE)
- {
- int c = compare_test(X1, X2, counter);
- if(c == 1 || c )
- {
- toggle_on(X3, counter);
- counter++;
- }
- else
- {
- counter++;
- }
- }
- return X3;
- }
- int *unify(int X1[SIZE], int X2[SIZE])
- {
- int X3[SIZE];
- int counter = 0;
- while(counter < 32 * SIZE)
- {
- int c = compare_test(X1, X2, counter);
- if(c > 0)
- {
- toggle_on(X3, counter);
- counter++;
- }
- else
- {
- counter++;
- }
- }
- return X3;
- }
- int visit_bit(int bitArray[SIZE], int k)
- {
- return 1 & (bitArray[k/32] >> (k%32));
- }
- void print_array(int *bitArray)
- {
- for(int i = 0; i < SIZE * 32; i++)
- {
- printf("%d ", visit_bit(bitArray, i));
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement