c语言编写双符号和进制位溢出怎么写
c语言编写双符号和进制位溢出怎么写 举例说明什么是所谓的 双符号和进制位溢出 什么意思? zhangjinxuan 发表于 2022-10-7 11:54什么意思?
就是用c语言编写溢出,给出两个二进制数让其相加减,看其是否大于1或者小于-1 2861913457 发表于 2022-10-16 16:29
就是用c语言编写溢出,给出两个二进制数让其相加减,看其是否大于1或者小于-1
意思是给出两个二进制数A,B,看相减大于1或小于-1? 本帖最后由 人造人 于 2022-10-16 23:49 编辑
2861913457 发表于 2022-10-16 16:29
就是用c语言编写溢出,给出两个二进制数让其相加减,看其是否大于1或者小于-1
#include <stdio.h>
#include <stdlib.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;
half_adder_1bit(a, b, &temp_s, &temp_c);
half_adder_1bit(temp_s, ci, s, &temp_c);
or_gate(temp_c, temp_c, co);
}
void full_adder_2bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_1bit(a, b, ci, &s, &temp_c);
full_adder_1bit(a, b, temp_c, &s, co);
}
void full_adder_4bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_2bit(&a, &b, ci, &s, &temp_c);
full_adder_2bit(&a, &b, temp_c, &s, co);
}
void full_adder_8bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_4bit(&a, &b, ci, &s, &temp_c);
full_adder_4bit(&a, &b, temp_c, &s, co);
}
void full_adder_16bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_8bit(&a, &b, ci, &s, &temp_c);
full_adder_8bit(&a, &b, temp_c, &s, co);
}
void full_adder_32bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_16bit(&a, &b, ci, &s, &temp_c);
full_adder_16bit(&a, &b, temp_c, &s, co);
}
void binary(char dest, 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 char *p = NULL;
for(size_t i = 0; i < 32; ++i) {
if(src == '1') {p = &src; 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 = '\0';
free(a_); free(b_);
return c;
}
void neg(char num) {
for(size_t i = 0; i < 32; ++i) {
xor_gate('1', num, &num);
}
char ci = '0', co;
char one = "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 = '\0';
free(a_); free(b_);
return c;
}
int main(void) {
char a;
char b;
scanf("%32s", a);
scanf("%32s", b);
char *c0 = add(a, b);
puts(c0);
if(c0 != '1') printf("大于1\n");
free(c0);
char *c1 = sub(a, b);
puts(c1);
if(c1 == '1' && c1 != '1') printf("小于-1\n");
free(c1);
return 0;
}
判断的部分不对,改一改
#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;
half_adder_1bit(a, b, &temp_s, &temp_c);
half_adder_1bit(temp_s, ci, s, &temp_c);
or_gate(temp_c, temp_c, co);
}
void full_adder_2bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_1bit(a, b, ci, &s, &temp_c);
full_adder_1bit(a, b, temp_c, &s, co);
}
void full_adder_4bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_2bit(&a, &b, ci, &s, &temp_c);
full_adder_2bit(&a, &b, temp_c, &s, co);
}
void full_adder_8bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_4bit(&a, &b, ci, &s, &temp_c);
full_adder_4bit(&a, &b, temp_c, &s, co);
}
void full_adder_16bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_8bit(&a, &b, ci, &s, &temp_c);
full_adder_8bit(&a, &b, temp_c, &s, co);
}
void full_adder_32bit(const char *a, const char *b, char ci, char *s, char *co) {
char temp_c;
full_adder_16bit(&a, &b, ci, &s, &temp_c);
full_adder_16bit(&a, &b, temp_c, &s, co);
}
void binary(char dest, 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 char *p = NULL;
for(size_t i = 0; i < 32; ++i) {
if(src == '1') {p = &src; 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 = '\0';
free(a_); free(b_);
return c;
}
void neg(char num) {
for(size_t i = 0; i < 32; ++i) {
xor_gate('1', num, &num);
}
char ci = '0', co;
char one = "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 = '\0';
free(a_); free(b_);
return c;
}
uint32_t number(const char n) {
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;
char b;
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;
}
我为什么不这么写呢?很奇怪
int main(void) {
char a;
char b;
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;
}
人造人 发表于 2022-10-17 00:12
我为什么不这么写呢?很奇怪
{:5_109:}你是写完了前面的才发现可以更简单,还是写一半时发现的?最恶心的是写一半时发现,是选择继续完成啊?还是重开?对于选择困难症的人太恶心了{:5_104:} jhq999 发表于 2022-10-17 07:38
你是写完了前面的才发现可以更简单,还是写一半时发现的?最恶心的是写一半时发现,是选择继续 ...
printf("%d\n", (int32_t)cu1);
写完发现的,这条语句发现的
如果写一半发现不对,那就及时纠正
错误要及时纠正,不要一错再错
不重开,在已经写了的代码的基础上修改就好
如果你发现没办法在已有的代码的基础上修改
那说明你一开始在考虑这个问题的时候想的不够全面,或者没有给可能会发生的那些变化留出足够的余地
这是你的设计问题
写代码之前应该得想好,你得有这个程序的整体布局 人造人 发表于 2022-10-17 13:28
printf("%d\n", (int32_t)cu1);
写完发现的,这条语句发现的
受教了
页:
[1]