八位全加器——python逻辑电路

逻辑电路(二)

1.半加器(HalfAdder)

2.全加器 (FullAdder)

3.二位全加器(Bi-FullAdder)

4.八位全加器(O-FullAdder)

 5.完整代码(Complete code)

6.参考资料(reference material)


在与或非门——python逻辑电路中,我们已经创建了与或非门、连接器等类。于是,在此基础上来创建半加器全加器,进而实现八位全加器

1.半加器(HalfAdder)

半加器是指对两个输入数据位相加,输出一个本位S(结果位)和进位S,没有进位输入的加法器电路。 是实现两个一位二进制数的加法运算电路。

其实现和真值表如下: ​​​​​​​​​​​​​​​​

​​​​​​​

我们需要创建变量pinA、pinB接收输入值,输入值用以异或门(XOR)与门(AND)共用。

同时,异或门的输出值即为本位S,与门的输出值即为进位C。

class HalfAdder:
    """半加器"""
    def __init__(self, n, pinA=None, pinB=None):
        self.label = n
        self.pinA = pinA    # 接收输入值pinA
        self.pinB = pinB    # 接收输入值pinB
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):    # 将pinA、pinB作为XOR和AND的输入值
        n1 = XORGate(self.label, self.pinA, self.pinB)
        self.S = n1.getoutput()    # 异或门的输出值即为本位S
        n2 = ANDGate("n2", self.pinA, self.pinB)
        self.C = n2.getoutput()    # 与门的输出值即为进位C
        return self.C, self.S

“”“测试”“”
h1 = HalfAdder("h1", 1, 1)
print(h1.getoutput())
# 输出结果为(1,0)

在创建了半加器的基础上,实现考虑全加器。

2.全加器 (FullAdder)

全加器是用门电路实现两个二进制数相加并求出和的组合线路,称为一位全加器。一位全加器可以处理低位进位,并输出本位S和进位C。

其实现和真值表如下:

全加器由两个半加器构成,同时半加器1的本位S1和低位的进位C0作为半加器2的输入值;半加器1的进位C1与 半加器2的进位C2作为或门1的输入值,或门1的输出值即是全加器的进位C。

class FullAdder:
    """全加器"""
    def __init__(self, n, c, pinA=None, pinB=None):
        self.label = n
        self.c = c
        self.pinA = pinA
        self.pinB = pinB
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        h1 = HalfAdder(self.label, self.pinA, self.pinB)
        h1.getoutput()
        h2 = HalfAdder(self.label)
        h2.pinA = h1.S    # h1的本位作为h2的pinA
        h2.pinB = self.c  # 低位的进位C0作为h2的pinB
        h2.getoutput()
        g1 = ORGate("g1", h1.C, h2.C)    # h1的进位C1与h2的进位C2作为g1的输入值
        g1.getoutput()
        self.S = h2.S
        self.C = g1.output
        return self.C, self.S    # 输出进位和本位


"""测试"""
f1 = FullAdder("f1", 0, 1, 1)
print(f1.getoutput())
# 输出结果为(1, 0)

3.二位全加器(Bi-FullAdder)

通过串联两个全加器,来实现二位全加器。将全加器f1的进位C1传递给全加器f2。

欲实现二进制数A、B的相加,则输入数A的第一位A1作为f1的pinA,输入数B的第一位B1作为f1的pinB;输入数A的第二位A2作为f2的pinA,输入数B的第二位B2作为f2的pinB。

 

class BiFullAdder:
    def __init__(self, n, A1=None, A2=None, B1=None, B2=None, c0=0):
        self.label = n
        self.c = c0
        self.A1 = A1
        self.A2 = A2
        self.B1 = B1
        self.B2 = B2
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        f1 = FullAdder(self.label+"1", self.c, self.A2, self.A1)
        f1.getoutput()
        f2 = FullAdder(self.label+"2", f1.C, self.B2, self.B1)
        f2.getoutput()
        self.C = f2.C
        self.S = f2.S
        return self.C, self.S, f1.S

“”“”测试“”“
bif1 = BiFullAdder("bif1", 1, 0, 1, 1)
print(bif1.getoutput())
# 输出结果为(1, 0, 1)

4.八位全加器(O-FullAdder)

同理串联8个全加器实现八位全加器。 

class OFullAdder:
    def __init__(self, n, N1=None, N2=None, c0=0):
        self.label = n
        self.c = c0
        self.N11 = int(str(N1)[-1])    # 由于数位过多,这里简化了参数的传入。
        self.N12 = int(str(N1)[-2])    # 使得直接传入两个二进制数字能够自动传递到所对应位。
        self.N13 = int(str(N1)[-3])
        self.N14 = int(str(N1)[-4])
        self.N15 = int(str(N1)[-5])
        self.N16 = int(str(N1)[-6])
        self.N17 = int(str(N1)[-7])
        self.N18 = int(str(N1)[-8])
        self.N21 = int(str(N2)[-1])
        self.N22 = int(str(N2)[-2])
        self.N23 = int(str(N2)[-3])
        self.N24 = int(str(N2)[-4])
        self.N25 = int(str(N2)[-5])
        self.N26 = int(str(N2)[-6])
        self.N27 = int(str(N2)[-7])
        self.N28 = int(str(N2)[-8])
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        f1 = FullAdder(self.label+"1", self.N11, self.N21, self.c)
        f1.getoutput()
        f2 = FullAdder(self.label+"2", self.N12, self.N22, f1.C)
        f2.getoutput()
        f3 = FullAdder(self.label+"3", self.N13, self.N23, f2.C)
        f3.getoutput()
        f4 = FullAdder(self.label+"4", self.N14, self.N24, f3.C)
        f4.getoutput()
        f5 = FullAdder(self.label+"5", self.N15, self.N25, f4.C)
        f5.getoutput()
        f6 = FullAdder(self.label+"6", self.N16, self.N26, f5.C)
        f6.getoutput()
        f7 = FullAdder(self.label+"7", self.N17, self.N27, f6.C)
        f7.getoutput()
        f8 = FullAdder(self.label+"8", self.N18, self.N28, f7.C)
        f8.getoutput()
        self.C = f8.C
        self.S = f8.S
        return self.C, self.S, f7.S, f6.S, f5.S, f4.S, f3.S, f2.S, f1.S


“”“测试”“”
of = OFullAdder("of", 11101101, 10110000)
print(of.getoutput())
# 输出结果为(1, 1, 0, 0, 1, 1, 1, 0, 1)

 5.完整代码(Complete code)

各类门电路实现详见 与或非门——python逻辑电路

class FullAdder:
    """全加器"""
    def __init__(self, n, c, pinA=None, pinB=None):
        self.label = n
        self.c = c
        self.pinA = pinA
        self.pinB = pinB
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        h1 = HalfAdder(self.label, self.pinA, self.pinB)
        h1.getoutput()
        h2 = HalfAdder(self.label)
        h2.pinA = h1.S
        h2.pinB = self.c
        h2.getoutput()
        g1 = ORGate("g1", h1.C, h2.C)
        g1.getoutput()
        self.S = h2.S
        self.C = g1.output
        return self.C, self.S


# f1 = FullAdder("f1", 0, 1, 1)
# print(f1.getoutput())


class BiFullAdder:
    def __init__(self, n, A1=None, A2=None, B1=None, B2=None, c0=0):
        self.label = n
        self.c = c0
        self.A1 = A1
        self.A2 = A2
        self.B1 = B1
        self.B2 = B2
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        f1 = FullAdder(self.label+"1", self.c, self.A2, self.A1)
        f1.getoutput()
        f2 = FullAdder(self.label+"2", f1.C, self.B2, self.B1)
        f2.getoutput()
        self.C = f2.C
        self.S = f2.S
        return self.C, self.S, f1.S


# bif1 = BiFullAdder("bif1", 1, 0, 1, 1)
# print(bif1.getoutput())


class OFullAdder:
    def __init__(self, n, N1=None, N2=None, c0=0):
        self.label = n
        self.c = c0
        self.N11 = int(str(N1)[-1])
        self.N12 = int(str(N1)[-2])
        self.N13 = int(str(N1)[-3])
        self.N14 = int(str(N1)[-4])
        self.N15 = int(str(N1)[-5])
        self.N16 = int(str(N1)[-6])
        self.N17 = int(str(N1)[-7])
        self.N18 = int(str(N1)[-8])
        self.N21 = int(str(N2)[-1])
        self.N22 = int(str(N2)[-2])
        self.N23 = int(str(N2)[-3])
        self.N24 = int(str(N2)[-4])
        self.N25 = int(str(N2)[-5])
        self.N26 = int(str(N2)[-6])
        self.N27 = int(str(N2)[-7])
        self.N28 = int(str(N2)[-8])
        self.S = None
        self.C = None

    def label(self):
        return self.label

    def getoutput(self):
        f1 = FullAdder(self.label+"1", self.N11, self.N21, self.c)
        f1.getoutput()
        f2 = FullAdder(self.label+"2", self.N12, self.N22, f1.C)
        f2.getoutput()
        f3 = FullAdder(self.label+"3", self.N13, self.N23, f2.C)
        f3.getoutput()
        f4 = FullAdder(self.label+"4", self.N14, self.N24, f3.C)
        f4.getoutput()
        f5 = FullAdder(self.label+"5", self.N15, self.N25, f4.C)
        f5.getoutput()
        f6 = FullAdder(self.label+"6", self.N16, self.N26, f5.C)
        f6.getoutput()
        f7 = FullAdder(self.label+"7", self.N17, self.N27, f6.C)
        f7.getoutput()
        f8 = FullAdder(self.label+"8", self.N18, self.N28, f7.C)
        f8.getoutput()
        self.C = f8.C
        self.S = f8.S
        return self.C, self.S, f7.S, f6.S, f5.S, f4.S, f3.S, f2.S, f1.S


# of = OFullAdder("of", 11101101, 10110000)
# print(of.getoutput())

6.参考资料(reference material)

https://baike.baidu.com/item/半加器/7653973?fr=aladdin

​​​​​​https://baike.baidu.com/item/全加器/9791810?fr=aladdin

与或非门——python逻辑电路_Smell as Sweet的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值