#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.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';
}
char *add(const char *a, const char *b) {
char *a_ = malloc(32), *b_ = malloc(32);
char *c = malloc(33);
binary(a_, a); binary(b_, b);
char ci = '0', co;
full_adder_32bit(a_, b_, ci, c, &co);
//format(c, c);
c[32] = '\0';
free(a_); free(b_);
return c;
}
void neg(char num[const 32]) {
for(size_t i = 0; i < 32; ++i) {
xor_gate('1', num[i], &num[i]);
}
char ci = '0', co;
char one[32] = "00000000000000000000000000000001";
full_adder_32bit(num, one, ci, num, &co);
}
char *sub(const char *a, const char *b) {
char *a_ = malloc(32), *b_ = malloc(32);
char *c = malloc(33);
binary(a_, a); binary(b_, b);
neg(b_);
char ci = '0', co;
full_adder_32bit(a_, b_, ci, c, &co);
//format(c, c);
c[32] = '\0';
free(a_); free(b_);
return c;
}
uint32_t number(const char n[const 32]) {
uint32_t rv = 0;
const char *p = n;
for(uint32_t mask = 0x80000000; mask; mask >>= 1) {
if(*p++ == '1') rv |= mask;
}
return rv;
}
int main(void) {
char a[33];
char b[33];
scanf("%32s", a);
scanf("%32s", b);
char *c0 = add(a, b);
puts(c0);
uint32_t cu0 = number(c0);
printf("%u\n", cu0);
if(cu0 > 1 && cu0 < 0x80000000) printf("大于1\n");
free(c0);
char *c1 = sub(a, b);
puts(c1);
uint32_t cu1 = number(c1);
printf("%d\n", (int32_t)cu1);
if(cu1 >= 0x80000000 && cu1 < 0xffffffff) printf("小于-1\n");
free(c1);
return 0;
}