Four bit adder: Difference between revisions

→‎{{header|Python}}: Add commentary.
m (→‎{{header|Sather}}: note about pin and (future) possibility about latch; Go's got it, it should can...)
(→‎{{header|Python}}: Add commentary.)
Line 283:
 
=={{header|Python}}==
Individual boolean bits are represented by either 1, 0, True (interchangeable with 1), and False (same as zero). a bit of value None is sometimes used as a place-holder.
 
Python functions represent the building blocks of the circuit: a function parameter for each block intput and individual block outputs are either a return of a single value or a return of a tuple of values - one for each block output. A single element tuple is ''not'' returned for a block with one output.
 
Python lists are used to represent bus's of multiple bits, and in this circuit, bit zero - the least significant bit of bus's, is at index zero of the list, (which will be printed as the ''left-most'' member of a list).
 
The repetitive connections of the full adder block, fa4, are achieved by using a for loop which could easily be modified to generate adders of any width. fa4's arguments are interpreted as indexable, ordered collections of values - usually lists but tuples would work too. fa4's outputs are the sum, s, as a list and the single bit carry.
 
Functions are provided to convert between integers and bus's and back; and the test routine shows how they can be used to translate between the normal Python values and those of the simulation.
<lang python>def xor(a, b): return (a and not b) or (b and not a)
 
Anonymous user