Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Write three Scheme procedures to simulate these three gates: AND, OR, and XOR,

ID: 3817470 • Letter: 1

Question

1 Write three Scheme procedures to simulate these three gates: AND, OR, and XOR, shown in the diagram in Figure 1. Test your procedures using all possible input combinations. (6 points)

Figure 1. Logic gates AND, OR, and XOR

2 Define a Scheme procedure (bitAdder x a b) to simulate the logic design given in the diagram in Figure 2. The procedure must call the gate procedures that you defined in Question 1 and must return a pair with two elements '(s . c), where s is the binary sum of a, b, and x, while c is the carry-out. You will implement the procedure in three steps using three procedures, as listed below.

2.1 Write a procedure (sum-bit x a b) to generate the result bit s. (5 points)

2.2 Write a procedure (carry-out x a b) to generate the carry-out bit c. (5 points)

2.3 Write a procedure (bitAdder x a b) to generate the pair out put (s . c). (4 points)

Figure 2. The logic design of a one-bit adder

Verify your procedure by exhaustive testing: Use all valid inputs to test the procedure. There are eight valid inputs:

(bitAdder 0 0 0)

(bitAdder 0 0 1)

(bitAdder 0 1 0)

(bitAdder 0 1 1)

(bitAdder 1 0 0)

(bitAdder 1 0 1)

(bitAdder 1 1 0)

(bitAdder 1 1 1)

The expected outputs are as follows:

;(0 . 0)

;(1 . 0)

;(1 . 0)

;(0 . 1)

;(1 . 0)

;(0 . 1)

;(0 . 1)

;(1 . 1)

Append your test outputs at the end of your procedures. Put the outputs as comments, so that your procedures can execute without removing these outputs.

a a a c b c --b c -b

Explanation / Answer

;1

(define

(AND_GATE a b)

(if (= a 1)

b

0))

;>(AND_GATE 0 0)

;OR

(define

(OR_GATE a b)

(if (= a 1)

1

b))

;>(OR_GATE 1 0)

;XOR

(define

(XOR_GATE a b)

(if (= a b)

0

1))

;>(XOR_GATE 1 1)

;2.1

(define

(sum-bit x a b)

( XOR_GATE x (XOR_GATE a b)))

;>(sum-bit 1 0 1)

;2.2

(define

(carry-out x a b)

(OR_GATE (AND_GATE a b)(AND_GATE (OR_GATE a b) x)))

;>(carry-out 1 0 1)

;2.3

(define

(bitAdder x a b)

(let ((s (sum-bit x a b))

(c (carry-out x a b)))

(list s c)))

>(bitAdder 1 0 1)