> For the complete documentation index, see [llms.txt](https://gittest2121.gitbook.io/nand2tetris/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gittest2121.gitbook.io/nand2tetris/combinational-chips/dmux4way-chip.md).

# DMux4Way Chip

### DMux4Way (4-way Demultiplexor) Chip

An 4-way Demultiplexor chip outputs the given input to certain output pin, which is specified by selector bit.

{% hint style="info" %}
An m-way n-bit demultiplexor channels a single n-bit input into one of m possible n-bit outputs. The selection is specified by a set of k control bits, where k = (log m) to the base 2.
{% endhint %}

```nand2tetris-hdl
Chip name: DMux4Way
Inputs: in, sel[2]
Outputs: a, b, c, d
Function: If sel=00 then {a=in, b=c=d=0}
                else if sel=01 then {b=in, a=c=d=0}
                else if sel=10 then {c=in, a=b=d=0}
                else if sel=11 then {d=in, a=b=c=0}.
```

![Abstraction of 4-way Demultiplexor Chip - Representation and Truth Table](/files/FnpIdmEXY9A317Mjf5Lz)

### Implementation of DMux4Way Chip in HDL

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

```nand2tetris-hdl
CHIP DMux4Way {
    IN in, sel[2];
    OUT a, b, c, d;

    PARTS:
    DMux(in=in, sel=sel[1], a=ab, b=cd);
    DMux(in=ab, sel=sel[0], a=a, b=b);
    DMux(in=cd, sel=sel[0], a=c, b=d);
}
```

### Implementation of DMux4Way Chip in Java™

*Similar to the Implementation in HDL*

```java
package CombChips;

class DMux4Way_Gate extends DMux_Gate {
    
    protected static int[] DMux4Way(int in, int[] sel) {
        int[] arr_sel = new int[2];

        int[] temp = DMux(in, arr_sel[0]);
        int[] temp1 = DMux(temp[0], arr_sel[1]);
        int[] temp2 = DMux(temp[1], arr_sel[1]);

        int[] out = new int[4];
        System.arraycopy(temp1, 0, out, 0, temp1.length);
        System.arraycopy(temp2, 0, out, temp1.length, temp2.length);

        return out;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gittest2121.gitbook.io/nand2tetris/combinational-chips/dmux4way-chip.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
