m0_74781001 2022-12-06 05:09 采纳率: 66.7%
浏览 107
已结题

关于overflow,ALU

有两道题不会,图片1-2为question1,3-5为question2
question1

img

img

question2

img

img

img

  • 写回答

2条回答 默认 最新

  • 一切因为有你 2022-12-06 11:04
    关注
    ##1
    
    def and_gate_32(a,b,c):
        return tuple([int(v) & int(b[i]) & int(c[i]) for i,v in enumerate(a)])
    
    def add_sub_overflow32(a,b,control):
        r1,r2=addsub32(a,b,control)
        a = a.zfill(32)
        b = b.zfill(32)
        if control==1:
            r3=1 if r1[0]!=a[0] else 0
        else:
            r3=1 if a[0]!=r1[0] and b[0]!=r1[0] else 0
        return r1,r2,r3
    
    ##2
    def alu(a,b,c1,c0):
        if len(a)>32 or len(b)>32 or c1 not in [0,1] or c0 not in [0,1]:
            return "input error"
        a=''.join().zfill(32)
        b=''.join().zfill(32)
        if c1==0 and c0==0:
            res=or_32bit(a,b)
            r1=1 if ''.join(res)=='0'.zfill(32) else 0
            r2=1 if res[0]==1 else 0
            return r1,r2,0,0
        elif c1==0 and c0==1:
            res=and_32bit(a,b)
            r1=1 if ''.join(res)=='0'.zfill(32) else 0
            r2=1 if res[0]==1 else 0
            return r1,r2,0,0
        elif c1==1 and c0==0:
            res,r3,r4=add_sub_overflow32(a,b,0)
            r1=1 if res=='0'.zfill(32) else 0
            r2=1 if res[0]==1 else 0
            return r1,r2,r3,r4
        elif c1==1 and c0==1:
            res,r3,r4=add_sub_overflow32(a,b,1)
            r1=1 if res=='0'.zfill(32) else 0
            r2=1 if res[0]==1 else 0
            return r1,r2,r3,r4
        else:
            return "input error"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月14日
  • 已采纳回答 12月6日
  • 创建了问题 12月6日