# Three-state logic

In digital electronics, a tri-state or three-state buffer is a type of digital buffer that has three stable states: a high voltage output state (logical 1), a low output state (logical 0), and a high-impedance (Hi-Z) state. In the Hi-Z state, the output of the buffer is effectively disconnected from the subsequent circuit.
Tri-state buffers are commonly used in bus-based systems where multiple devices are connected to the same shared bus, because the Hi-Z state allows other devices to drive the bus without interference from the tri-state buffer. For example, in a computer system, multiple devices such as the CPU, memory, and peripherals may be connected to the same data bus. To ensure that only one device can transmit data on the bus at a time, each device is equipped with a tri-state buffer. When a device wants to transmit data, it activates its tri-state buffer, which connects its output to the bus and allows it to transmit data. When the transmission is complete, the device deactivates its tri-state buffer, which disconnects its output from the bus and allows another device to access the bus. Tri-state buffers are also useful for reducing crosstalk and noise on a bus.
Tri-state output can be incorporated into various logic gates, flip-flops, microcontrollers, or other digital logic circuits.

## Operation

A tri-state buffer behaves either like an open switch (i.e. presenting a Hi-Z output) when the enable signal B is off or as a regular non-inverting buffer (which duplicates and boosts the input onto the output) when the enable signal is on:
Unlike a simple transmission gate, a tri-state buffer when enabled additionally provides voltage level restoration to boost the input to be well within its valid logic voltage range. A tri-state buffer's behavior is given by the following truth table:
    Input Output A B C 0 0 Hi-Z 1 Hi-Z 0 1 0 1 1
Alternatively, inverting tri-state buffers when enabled will invert the input A.

## Uses

The Hi-Z state's purpose is to effectively remove a device's influence from the rest of the circuit. If multiple devices output to a shared wire, no device should drive the shared wire to one logical voltage level when another device drives the shared wire to another logical voltage level, since that competition would result in excessive current draw through the short circuit and an uncertain voltage level.
Three-state devices on many shared electronic buses present a Hi-Z output when not actively communicating, so that shared wires are available to be driven. For example, in a Serial Peripheral Interface bus in multidrop configuration, only a single peripheral chip at a time may be selected using its CS (chip select), while all other peripheral chips present a Hi-Z output to avoid corrupting the data sent by the one selected chip. Three-state buses are typically used between chips on a single printed circuit board (PCB), or sometimes between PCBs plugged into a common backplane.
Three-state buffers can also be used to implement efficient multiplexers, especially those with large numbers of inputs.
Tri-state multiplexing, also known as Charlieplexing, is used to reduce the number of wires needed to drive a set of light-emitting diodes.

## Output enable vs. chip select

Many memory devices designed to connect to a bus (such as RAM and ROM chips) have both CS and OE (output enable) pins, which superficially appear to do the same thing. If CS is not asserted, the outputs are Hi-Z.
The difference lies in the time needed to output the signal. When chip select is deasserted, the chip does not operate internally, and there will be a significant delay between providing an address and receiving the data. (An advantage of course, is that the chip consumes minimal power in this case.)
When chip select is asserted, the chip internally performs the access, and only the final output drivers are disabled by deasserting output enable. This can be done while the bus is in use for other purposes, and when output enable is finally asserted, the data will appear with minimal delay. A ROM or static RAM chip with an output enable line will typically list two access times: one from chip select asserted and address valid, and a second, shorter time beginning when output enable is asserted.

## Use of pull-ups and pull-downs

When outputs are tri-stated (in the Hi-Z state) their influence on the rest of the circuit is removed, and the circuit node will be "floating" if no other circuit element determines its state. Circuit designers will often use pull-up or pull-down resistors (usually within the range of 1–100 kΩ) to influence the circuit when the output is tri-stated.
The PCI local bus provides pull-up resistors, but they would require several clock cycles to pull a signal high given the bus's large distributed capacitance. To enable high-speed operation, the protocol requires that every device connecting to the bus drive the important control signals high for at least one clock cycle before going to the Hi-Z state. This way, the pull-up resistors are only responsible for maintaining the bus signals in the face of leakage current. Intel refers to this convention as "sustained tri-state", and also uses it in the Low Pin Count bus.

## Alternatives

Open collector output is an alternative to three-state logic. For example, the bidirectional I²C bus uses pull-up resistors on its two shared communication lines. When not transmitting, devices present a Hi-Z output to effectively "release" control over the communication lines. When all devices on the bus are Hi-Z, the only influence on the circuit is the pull-up resistors, which pull the lines high. When a device wants to transmit, it comes out of the Hi-Z state and drives the line low. Devices communicating over I²C either let the line float high, or drive it low – thus preventing any bus contention situation where one device drives a line high and another low.
Early microcontrollers often have some pins that can only act as an input, other pins that can only act as a push–pull output, and a few pins that can only act as an open collector input/output. A typical modern microcontroller has many three-state general-purpose input/output pins that can be programmed to act as any of those kinds of pins.
Usage of three-state logic is not recommended for on-chip connections but rather for inter-chip connections. Three-state buffers, when used to enable multi-device communication on a data bus, can be functionally replaced by a multiplexer. That will help select output from a range of devices and write one to the bus.