How to resolve the algorithm Four bit adder step by step in the UNIX Shell programming language
How to resolve the algorithm Four bit adder step by step in the UNIX Shell 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 UNIX Shell programming language
Source code in the unix programming language
xor() {
typeset -i a=$1 b=$2
printf '%d\n' $(( (a || b) && ! (a && b) ))
}
half_adder() {
typeset -i a=$1 b=$2
printf '%d %d\n' $(xor $a $b) $(( a && b ))
}
full_adder() {
typeset -i a=$1 b=$2 c=$3
typeset -i ha0_s ha0_c ha1_s ha1_c
read ha0_s ha0_c < <(half_adder "$c" "$a")
read ha1_s ha1_c < <(half_adder "$ha0_s" "$b")
printf '%d %d\n' "$ha1_s" "$(( ha0_c || ha1_c ))"
}
four_bit_adder() {
typeset -i a0=$1 a1=$2 a2=$3 a3=$4 b0=$5 b1=$6 b2=$7 b3=$8
typeset -i fa0_s fa0_c fa1_s fa1_c fa2_s fa2_c fa3_s fa3_c
read fa0_s fa0_c < <(full_adder "$a0" "$b0" 0)
read fa1_s fa1_c < <(full_adder "$a1" "$b1" "$fa0_c")
read fa2_s fa2_c < <(full_adder "$a2" "$b2" "$fa1_c")
read fa3_s fa3_c < <(full_adder "$a3" "$b3" "$fa2_c")
printf '%s' "$fa0_s"
printf ' %s' "$fa1_s" "$fa2_s" "$fa3_s" "$fa3_c"
printf '\n'
}
is() {
typeset label=$1
shift
if eval "$*"; then
printf 'ok'
else
printf 'not ok'
fi
printf ' %s\n' "$label"
}
is "1 + 1 = 2" "[[ '$(four_bit_adder 1 0 0 0 1 0 0 0)' == '0 1 0 0 0' ]]"
is "5 + 5 = 10" "[[ '$(four_bit_adder 1 0 1 0 1 0 1 0)' == '0 1 0 1 0' ]]"
is "7 + 9 = overflow" "a=($(four_bit_adder 1 0 0 1 1 1 1 0)); (( \${a[-1]}==1 ))"
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Entropy step by step in the REXX programming language
You may also check:How to resolve the algorithm Color quantization step by step in the J programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the VBA programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Kamailio Script programming language