How to resolve the algorithm Four bit adder step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Four bit adder step by step in the Common Lisp programming language

Table of Contents

Problem Statement

"Simulate" a four-bit adder. This design can be realized using four 1-bit full adders. Each of these 1-bit full adders can be built with two half adders and an   or   gate. ; Finally a half adder can be made using an   xor   gate and an   and   gate. The   xor   gate can be made using two   nots,   two   ands   and one   or. Not,   or   and   and,   the only allowed "gates" for the task, can be "imitated" by using the bitwise operators of your language. If there is not a bit type in your language, to be sure that the   not   does not "invert" all the other bits of the basic type   (e.g. a byte)   we are not interested in,   you can use an extra   nand   (and   then   not)   with the constant   1   on one input. Instead of optimizing and reducing the number of gates used for the final 4-bit adder,   build it in the most straightforward way,   connecting the other "constructive blocks",   in turn made of "simpler" and "smaller" ones.

Solutions should try to be as descriptive as possible, making it as easy as possible to identify "connections" between higher-order "blocks". It is not mandatory to replicate the syntax of higher-order blocks in the atomic "gate" blocks, i.e. basic "gate" operations can be performed as usual bitwise operations, or they can be "wrapped" in a block in order to expose the same syntax of higher-order blocks, at implementers' choice. To test the implementation, show the sum of two four-bit numbers (in binary).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Four bit adder step by step in the Common Lisp programming language

Source code in the common programming language

;; returns a list of bits: '(sum carry)
(defun half-adder (a b)
  (list (logxor a b) (logand a b)))

;; returns a list of bits: '(sum, carry)
(defun full-adder (a b c-in)
  (let*
    ((h1 (half-adder c-in a))
    (h2 (half-adder (first h1) b)))
    (list (first h2) (logior (second h1) (second h2)))))

;; a and b are lists of 4 bits each
(defun 4-bit-adder (a b)
  (let*
    ((add-1 (full-adder (fourth a) (fourth b) 0))
      (add-2 (full-adder (third a) (third b) (second add-1)))
      (add-3 (full-adder (second a) (second b) (second add-2)))
      (add-4 (full-adder (first a) (first b) (second add-3))))
    (list
      (list (first add-4) (first add-3) (first add-2) (first add-1))
      (second add-4))))

(defun main ()
  (print (4-bit-adder (list 0 0 0 0) (list 0 0 0 0)))   ;; '(0 0 0 0) and 0
  (print (4-bit-adder (list 0 0 0 0) (list 1 1 1 1)))   ;; '(1 1 1 1) and 0
  (print (4-bit-adder (list 1 1 1 1) (list 0 0 0 0)))   ;; '(1 1 1 1) and 0
  (print (4-bit-adder (list 0 1 0 1) (list 1 1 0 0)))   ;; '(0 0 0 1) and 1
  (print (4-bit-adder (list 1 1 1 1) (list 1 1 1 1)))   ;; '(1 1 1 0) and 1
  (print (4-bit-adder (list 1 0 1 0) (list 0 1 0 1)))   ;; '(1 1 1 1) and 0
 )

(main)


  

You may also check:How to resolve the algorithm Random number generator (included) step by step in the 8th programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the D programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Rust programming language