/* * bitops.c * * carries out some bit operations on strings. */ #include #include #include int a [80], b [80], c [80]; void or (void); void and (void); void xor (void); void print (void); void setup (char *s1, char *s2); int main (void) { char s1 [85], s2 [85], op; while (gets (s1) != NULL) { gets (s2); op = s2 [0]; gets (s2); setup (s1, s2); switch (op) { case 'O': or (); break; case 'X': xor (); break; case 'A': and (); break; } print (); } return 0; } void or (void) { int i; for (i = 0; i < 80; i++) { if (a [i] == 1 || b [i] == 1) c [i] = 1; else c [i] = 0; } } void and (void) { int i; for (i = 0; i < 80; i++) { if (a [i] == 1 && b [i] == 1) c [i] = 1; else c [i] = 0; } } void xor (void) { int i; for (i = 0; i < 80; i++) { if (a [i] == 1 && b [i] == 1) c [i] = 0; else if (a [i] == 0 && b [i] == 0) c [i] = 0; else c [i] = 1; } } void print (void) { int i, flag; flag = 0; for (i = 0; i < 80; i++) { if (c [i] == 1) flag = 1; if (flag) printf ("%d", c [i]); } if (!flag) printf ("0"); printf ("\n"); } void setup (char *s1, char *s2) { int i, j; for (i = 0; i < 80; i++) { a [i] = 0; b [i] = 0; c[i] = 0; } j = 79; for (i = strlen (s1) - 1; i >= 0; i--) { a [j] = s1 [i] - '0'; j--; } j = 79; for (i = strlen (s2) - 1; i >= 0; i--) { b [j] = s2 [i] - '0'; j--; } }