How to resolve the algorithm Four bit adder step by step in the Racket programming language
How to resolve the algorithm Four bit adder step by step in the Racket 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 Racket programming language
Source code in the racket programming language
#lang racket
(define (adder-and a b)
(if (= 2 (+ a b)) 1 0)) ; Defining the basic and function
(define (adder-not a)
(if (zero? a) 1 0)) ; Defining the basic not function
(define (adder-or a b)
(if (> (+ a b) 0) 1 0)) ; Defining the basic or function
(define (adder-xor a b)
(adder-or
(adder-and
(adder-not a)
b)
(adder-and
a
(adder-not b)))) ; Defines the xor function based on the basic functions
(define (half-adder a b)
(list (adder-xor a b) (adder-and a b))) ; Creates the half adder, returning '(sum carry)
(define (adder a b c0)
(define half-a (half-adder c0 a))
(define half-b (half-adder (car half-a) b))
(list
(car half-b)
(adder-or (cadr half-a) (cadr half-b)))) ; Creates the full adder, returns '(sum carry)
(define (n-bit-adder 4a 4b) ; Creates the n-bit adder, it receives 2 lists of same length
(let-values ; Lists of the form '([01]+)
(((4s v) ; for/fold form will return 2 values, receiving this here
(for/fold ((S null) (c 0)) ;initializes the full sum and carry
((a (in-list (reverse 4a))) (b (in-list (reverse 4b))))
;here it prepares variables for summing each element, starting from the least important bits
(define added
(adder a b c))
(values
(cons (car added) S) ; changes S and c to it's new values in the next iteration
(cadr added)))))
(if (zero? v)
4s
(cons v 4s))))
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)
You may also check:How to resolve the algorithm Make directory path step by step in the Ruby programming language
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the jq programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the E programming language