🔌
nand2tetris
  • Introduction
  • Combinational Chips
    • Nand Gate
    • Not Gate
    • And Gate
    • Or Gate
    • Xor Gate
    • Multiplexor Chip
    • Demultiplexor Chip
    • Not16 Chip
    • And16 Chip
    • Or16 Chip
    • Mux16 Chip
    • Or8Way Chip
    • Mux4Way16 Chip
    • Mux8Way16 Chip
    • DMux4Way Chip
    • DMux8Way Chip
    • Half Adder Chip
    • Full Adder Chip
    • Add16 Chip
    • Inc16 Chip
    • Half Subtractor Chip
    • Full Subtractor Chip
    • Subtract16 Chip
    • Dec16 Chip
    • Arithmetic Chip
    • ALU
  • Misc
    • Int2Bool
    • Bool2Int
    • Arrayto16
Powered by GitBook
On this page
  • Subtractor Chip
  • Half Subtractor Chip
  • Implementation of Half Subtractor Chip in HDL
  • Implementation of Half Subtractor Chip in Java™
  1. Combinational Chips

Half Subtractor Chip

Abstraction and Implementation of Half Subtractor Chip in Hardware Design Language and Java™.

PreviousInc16 ChipNextFull Subtractor Chip

Last updated 3 years ago

Subtractor Chip

The chip used to sutract two n-bit numbers is known as Subtractor, also known as n-bit Subtractor.

Half Subtractor Chip

Half Subtractor chip is used to subtract 2-bits.

Chip name: HalfSubtractor
Inputs: a, b
Outputs: diff, borrow
Function: diff = LSB of a - b
          borrow = MSB of a - b
Abstraction of Half Subtractor Chip - Representation, Implementation and Truth Table

Implementation of Half Subtractor Chip in HDL

The function in the above abstraction can help in the implementation of Half Subtractor Chip.

diff = a XOR b

borrow = (NOT a) AND b

CHIP HalfSubtractor {
    IN a, b;    // 1-bit inputs
    OUT diff,    // Right bit of a + b 
        borrow;  // Left bit of a + b

    PARTS:
    Xor(a=a, b=b, out=diff);
    Not(in=a, out=nota);
    And(a=nota, b=b, out=borrow);
}

Implementation of Half Subtractor Chip in Java™

Similar to the Implementation in HDL

package CombChips;

class HalfSubtractor_Gate extends Xor_Gate {

    protected static int[] HalfSubtractor(int a, int b) {
        int[] out = new int[2];

        // sum
        out[0] = Xor(a, b);

        // borrow
        out[1] = And_Gate.And(Not(a), b);

        return out;
    }
}