🔌
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
  1. Misc

Arrayto16

Misc is a package in Java™ Implementation of nand2tetris. It contains basic utilites and reusable codes for creation and modification of chips and its outputs.

Arrayto16 Method in class Convert

Input: Integer Array

Output: Integer Array

    public static int[] Arrayto16(int[] in) {
        int[] inp16 = new int[16];

        System.arraycopy(in, 0, inp16, 0, in.length);
        // arraycopy is used so that leading zeros is present
        
        return inp16;
    }

Generally, you may encounter situations where you want to give an input of less than 16 bits or the value in the integer parameter has leading zeros, this may lead to ArrayIndexOutOfBoundsException. To avoid such issues, the method Arrayto16 is created, so that the size of the array is always 16. The arraycopy method from system is used to implement leading zeros.

Unlike nand2tetris, where HDL considers arrays from right to left, Java considers arrays from left to right.

PreviousBool2Int

Last updated 3 years ago