python - 半加器和全加器逻辑?

标签 python boolean-logic circuit

我无法通过半加器和全加器测试,我想知道 FullAdder() 中的逻辑有什么问题。和HalfAdder()方法?由于某种原因,我的测试似乎都没有通过......

代码如下:

    # All wires that enter/exit the circuit need to be visible outside of the 
    # function: they are to be created in the enclosing scope and passed 
    # as parameters.
    # However, wires that connect the AND gates to the OR gates are 
    # _internal wires_, visible only inside the circuit: they need to be created
    xy = self.Wire('xy')
    xz = self.Wire('xz')
    yz = self.Wire('yz')
    xy_or_xz = self.Wire('xy_or_xz')

    self.AndGate(x, y, xy)  
    self.AndGate(x, z, xz)  
    self.Andgate(x, z, yz)

    # OR(xy,xz,yz) = OR( OR(xy,xz),  yz)
    self.OrGate(xy, xz, xy_or_xz)
    self.OrGate(xy_or_xz, yz, o)

    return 'ok'

def TwoSwitches(self, x, y, o):
    """ An example: light controlled by 2 switches (Rosen, 12.3, Example 3,
        - Figure 6, p. 825)

        F(x, y) = xy + !x.!y
    """
    # Wires x, y, and o are passed as parameters.
    # Only the internal wires need to be created:
    xy = self.Wire('xy')
    not_x = self.Wire('not_x')
    not_y = self.Wire('not_y')
    notx_noty = self.Wire('notx_noty')

    self.AndGate(x,y,xy)
    self.Inverter(x, not_x)
    self.Inverter(y, not_y)
    self.AndGate(not_x, not_y, notx_noty)
    self.OrGate(xy, notx_noty, o)       

    return 'ok'

# This function needs to be defined: parameter, body definition
def HalfAdder(self, x, y, s, c):
            notx_and_y=self.wire('notx_and_y')
            x_and_noty=self.wire('x_and_noty')
            cwire=self.wire('cwire')
            allwire=self.wire(allwire)
            self.OrGate(self.AndGate(not x,y,notx_and_y),self.AndGate(x,not y,x_and_noty),allwire)
    cwire=self.AndGate(x,y,cwire)
    c=cwire
    s=allwire
    pass

def FullAdder(self,x,y,c_i,s, c_out):
    #TODO
    pass

最佳答案

半加器

半加器可以使用 AND 门和 XOR 门来实现:

  x    y
  |    |
  |------------------\
  |    |--------\    |
+--------+    +--------+
|  AND   |    |  XOR   |
+--------+    +--------+
    |             |
    c             s

所以实现是这样的:

def Xor(self, x, y, xor):
    nx = self.Wire('nx')
    ny = self.Wire('ny')
    self.Inverter(x,nx)
    self.Inverter(x,ny)
    a1 = self.Wire('a1')
    a2 = self.Wire('a2')
    self.AndGate(nx,y,a1)
    self.AndGate(x,ny,a2)
    self.OrGate(a1,a2,xor)
    pass

def HalfAdder(self, x, y, s, c):
    self.AndGate(x,y,c)
    self.Xor(x,y,s)

全加器

构建全加器的方法有多种,其中一种方法是使用两个半加法器,如我的类(class)文本 digital electronics 中所述。 (抱歉,荷兰语):

      x    y          c_i
      |    |           |
    +--------+         |
  /-|   HA   |         |
  | +--------+         |
  |     |              |
  |     `---------\    |
  |               |    |
  |             +--------+
  |    /--------|   HA   |
  |    |        +--------+
+--------+           |
|   OR   |           s
+--------+
    |
  c_out

上面有一个HalfAdderxy,左边是cs 底部。

所以这意味着:

def FullAdder(self,x,y,c_i,s, c_out):
    ha1s = self.Wire('ha1s')
    ha1c = self.Wire('ha1c')
    self.HalfAdder(x,y,ha1s,ha1c)
    ha2c = self.Wire('ha2c')
    self.HalfAdder(ha1s,c_i,s,ha2c)
    self.OrGate(ha1c,ha2c,c_out)
    pass

一般建议是始终将问题分解为更容易控制的较小子问题。

关于python - 半加器和全加器逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124935/

相关文章:

race-condition - 比赛条件

c++ - c中的自定义PCB闪烁灯

vhdl - VHDL 中的通用移位算法

python - Numpy,连接整数和 ndarrays 的简单公式

python - Django 表单中的可编辑选择字段/下拉框

java - 有没有一种方便的方法来检查是否至少有一定数量的条件为真?

scala - Future中的 bool 逻辑[Boolean]

c - 逻辑与运算符

python - 导入 Eve,但运行 run.py 时,显示没有名为 eve 的模块

java - 解释 vs. 编译 vs. 后期绑定(bind)