#include <stdio.h>
#include <string.h>
void and_gate(char a, char b, char *c) {
*c = a == '1' && b == '1' ? '1' : '0';
}
void or_gate(char a, char b, char *c) {
*c = a == '0' && b == '0' ? '0' : '1';
}
#if 0
void xor_gate(char a, char b, char *c) {
*c = a != b ? '1' : '0';
}
#else
void not_gate(char a, char *b) {
*b = a == '1' ? '0' : '1';
}
void xor_gate(char a, char b, char *c) {
char na, nb;
char r0, r1;
not_gate(a, &na);
not_gate(b, &nb);
and_gate(a, nb, &r0);
and_gate(b, na, &r1);
or_gate(r0, r1, c);
}
#endif
void half_adder_1bit(char a, char b, char *s, char *c) {
xor_gate(a, b, s);
and_gate(a, b, c);
}
void full_adder_1bit(char a, char b, char ci, char *s, char *co) {
char temp_s, temp_c[2];
half_adder_1bit(a, b, &temp_s, &temp_c[0]);
half_adder_1bit(temp_s, ci, s, &temp_c[1]);
or_gate(temp_c[0], temp_c[1], co);
}
void full_adder_2bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_1bit(a[1], b[1], ci, &s[1], &temp_c);
full_adder_1bit(a[0], b[0], temp_c, &s[0], co);
}
void full_adder_4bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_2bit(&a[2], &b[2], ci, &s[2], &temp_c);
full_adder_2bit(&a[0], &b[0], temp_c, &s[0], co);
}
void full_adder_8bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_4bit(&a[4], &b[4], ci, &s[4], &temp_c);
full_adder_4bit(&a[0], &b[0], temp_c, &s[0], co);
}
void full_adder_16bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_8bit(&a[8], &b[8], ci, &s[8], &temp_c);
full_adder_8bit(&a[0], &b[0], temp_c, &s[0], co);
}
void full_adder_32bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_16bit(&a[16], &b[16], ci, &s[16], &temp_c);
full_adder_16bit(&a[0], &b[0], temp_c, &s[0], co);
}
void binary(char dest[const 32], const char *src) {
memset(dest, '0', 32);
size_t l = strlen(src);
strncpy(dest + (32 - l), src, l);
}
void format(char *dest, const char src[const 32]) {
const char *p = NULL;
for(size_t i = 0; i < 32; ++i) {
if(src[i] == '1') {p = &src[i]; break;}
}
if(!p) p = src + 31;
while(p != src + 32) *dest++ = *p++;
*dest = '\0';
}
int main() {
char a[32];
char b[32];
char s[32];
char ci = '0', co;
char buff[33];
scanf("%32s", buff); binary(a, buff);
scanf("%32s", buff); binary(b, buff);
full_adder_32bit(a, b, ci, s, &co);
format(buff, s); puts(buff);
return 0;
}