1
votes

Define a Scheme procedure (bitAdder x a b) to simulate the logic design given in following 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.

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

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

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

So far I have defined my logic gates for "and", "or" , and "xor". I tried to do the first two procedures but they seem incorrect.

(define AND-gate (lambda (a b)
               (if (= a b 1)
                   1
                   0)))
(define OR-gate (lambda (a b)
              (if (= a 1)
                  1
                  (if (= b 1)
                      1
                      0))))
(define XOR-gate (lambda (a b)
               (if (= a b)
                   0
                   1)))
 (define sum-bit (lambda (x a b)
             (XOR-gate a b)
             (XOR-gate x (XOR-gate a b))))
  (define carry-out (lambda (x a b)
                (OR-gate a b)
                (AND-gate (OR-gate a b) x)
                (AND-gate a b)
                (OR-gate (AND-gate a b) (AND-gate (OR-gate a b) x))))
1
What about them "seems incorrect"? Describe the error, or what about the implementation doesn't behave as you're expecting.Ian
sum-bit evaluates (XOR-gate a b) and ignores that value, then returns (XOR-gate x (XOR-gate a b)). You follow the same "evaluate and throw away" pattern in carry-out.molbdnilo

1 Answers

1
votes

Define the Gates

#lang racket

(define (AND-gate A B)
  (if (= A B 1)
      1
      0))

(define (XOR-gate A B)
  (if (not (= A B))
      1
      0))

(define (OR-gate A B)
  (if (not (= 0 (+ A B)))
      1
      0))

Test the Gates

(module+ test
 (require rackunit
      rackunit/text-ui)

  (define-test-suite
    gate-tests
    (test-equal? "AND-gate" (AND-gate 1  1) 1)
    (test-equal? "XOR-gate" (XOR-gate 0  1) 1)
    (test-equal? "XOR-gate" (XOR-gate 1  0) 1)
    (test-equal? "XOR-gate" (XOR-gate 0  0) 0)
    (test-equal? "XOR-gate" (XOR-gate 1  1) 0)
    (test-equal? "AND-gate" (AND-gate 0  0) 0)
    (test-equal? "AND-gate" (AND-gate 0  1) 0)
    (test-equal? "AND-gate" (AND-gate 1  0) 0)
    (test-equal? "OR-gate"  (OR-gate  0  0) 0)
    (test-equal? "OR-gate"  (OR-gate  1  0) 1)
    (test-equal? "OR-gate"  (OR-gate  0  1) 1)
    (test-equal? "OR-gate"  (OR-gate  1  1) 1))

  (run-tests gate-tests))

Define Sum-Bit

(define (sum-bit X A B)
  (XOR-gate X (XOR-gate A B)))

Test Sum-Bit

(module+ test

  (define-test-suite
    sum-bit-tests
    (test-equal? "Sum-bit" (sum-bit 0 0 0) 0)
    (test-equal? "Sum-bit" (sum-bit 1 0 0) 1)
    (test-equal? "Sum-bit" (sum-bit 0 1 0) 1)
    (test-equal? "Sum-bit" (sum-bit 0 0 1) 1)
    (test-equal? "Sum-bit" (sum-bit 1 1 0) 0)
    (test-equal? "Sum-bit" (sum-bit 1 0 1) 0)
    (test-equal? "Sum-bit" (sum-bit 0 1 1) 0)
    (test-equal? "Sum-bit" (sum-bit 1 1 1) 1))

  (run-tests sum-bit-tests))

Define Carry-Bit

(define (carry-bit X A B)
  (OR-gate (AND-gate A B)
           (OR-gate (AND-gate X A)
                    (AND-gate X B))))

Test Carry-Bit

(module+ test
  (define-test-suite
    carry-bit-tests
    (test-equal? "Carry-bit" (carry-bit 0 0 0) 0)
    (test-equal? "Carry-bit" (carry-bit 1 0 0) 0)
    (test-equal? "Carry-bit" (carry-bit 0 1 0) 0)
    (test-equal? "Carry-bit" (carry-bit 0 0 1) 0)
    (test-equal? "Carry-bit" (carry-bit 1 1 0) 1)
    (test-equal? "Carry-bit" (carry-bit 1 0 1) 1)
    (test-equal? "Carry-bit" (carry-bit 0 1 1) 1)
    (test-equal? "Carry-bit" (carry-bit 1 1 1) 1))

  (run-tests carry-bit-tests))

Define Bit-Adder

(define (bit-adder X A B)
  (cons (sum-bit X A B)
    (carry-bit X A B)))

Test Bit-Adder

(module+ test
  (define-test-suite
    bit-adder-tests
    (test-equal? "Bit-adder" (bit-adder 0 0 0) '(0 . 0))
    (test-equal? "Bit-adder" (bit-adder 1 0 0) '(1 . 0))
    (test-equal? "Bit-adder" (bit-adder 0 1 0) '(1 . 0))
    (test-equal? "Bit-adder" (bit-adder 0 0 1) '(1 . 0))
    (test-equal? "Bit-adder" (bit-adder 1 1 0) '(0 . 1))
    (test-equal? "Bit-adder" (bit-adder 1 0 1) '(0 . 1))
    (test-equal? "Bit-adder" (bit-adder 0 1 1) '(0 . 1))
    (test-equal? "Bit-adder" (bit-adder 1 1 1) '(1 . 1)))

  (run-tests bit-adder-tests))

Run the Tests

[email protected]> ,enter "/home/ben/StackOverflow/297774346.rkt"
12 success(es) 0 failure(s) 0 error(s) 12 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0
8 success(es) 0 failure(s) 0 error(s) 8 test(s) run
0  

Turn in Homework

Subject to academic policies regarding the work of others.