$ ls
main.c
$ cat main.c
#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((int32_t)cu0 > 1) printf("大于1\n");
free(c0);
char *c1 = sub(a, b);
puts(c1);
uint32_t cu1 = number(c1);
printf("%d\n", (int32_t)cu1);
if((int32_t)cu1 < -1) printf("小于-1\n");
free(c1);
return 0;
}
$ gcc -g -Wall -shared -o main.dll main.c
$ ls
main.c main.dll
$ file main.dll
main.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows
$ python
Python 3.8.12 (default, Nov 23 2021, 20:18:25)
[GCC 11.2.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> dll = ctypes.cdll.LoadLibrary('main.dll')
>>> dll
<CDLL 'main.dll', handle 509660000 at 0x6fffffec79d0>
>>> dll.main()
10110110
1001
00000000000000000000000010111111
191
大于1
00000000000000000000000010101101
173
0
>>> dll.add(b'10011100', b'100101')
1633360
>>> dll.add.restype = ctypes.c_char_p
>>> dll.add(b'10011100', b'100101')
b'00000000000000000000000011000001'
>>>
$