What is One-hot encoding?

State encoding that assigns one flip-flop per state with exactly one set at a time. Logic tends to be simple and fast, and it is common on FPGAs.

Field: HDL & Verification

Japanese name: ワンホット符号化

How one-hot state encoding works

In a finite state machine, one-hot encoding gives each state its own flip-flop, and exactly one flip-flop holds 1 at any time. Four states become 0001, 0010, 0100 and 1000. Testing whether the machine is in a given state means looking at a single bit, so outputs that depend on the state are often just one flip-flop or a small OR of a few. The next-state input of each flip-flop is the OR of the conditions on the transitions entering that state, each ANDed with the bit of the source state, which keeps the logic depth shallow and the machine fast.

One-hot vs. binary encoding

Binary encoding numbers the states and needs only the ceiling of log2 N flip-flops: three states fit in two flip-flops, while one-hot needs three, and sixteen states need four flip-flops versus sixteen. The cost of binary encoding is wider decode logic, because recognizing a state means comparing all state bits. Gray-code encoding, a third option, assigns codes so that frequent transitions change one bit, reducing switching activity. Binary suits designs where flip-flops are scarce or the state count is large; one-hot suits speed and simplicity when flip-flops are plentiful.

Why FPGAs favor it

An FPGA logic block pairs each small lookup table (LUT) with one or more flip-flops, so registers are abundant while wide logic functions must be split across several LUT levels. One-hot encoding fits this structure well: each state bit's next-state function usually involves only a few inputs and fits in a single LUT, which shortens critical paths. Synthesis tools commonly detect state machines and pick an encoding automatically, often one-hot for small and medium machines, though designers can override the choice. In ASICs, where flip-flops cost more area, binary or other compact encodings are more common.

Illegal states and recovery

With N flip-flops, one-hot uses only N of the 2^N possible patterns, so all-zero and multi-hot patterns are illegal. Noise, a glitch or a radiation-induced bit flip could put the machine into one of them, where it may stall or behave unpredictably. Binary encoding has the same issue with unused codes, such as 11 in a three-state machine. Robust designs therefore define a default branch that returns any unexpected state to a known one, and synthesis tools offer safe state-machine options that keep this recovery logic instead of optimizing it away.

Try it: questions about One-hot encoding

What is a characteristic of one-hot encoding for state machine states?

  1. AIt encodes states in binary to minimize flip-flops
  2. BAll flip-flops are always 1
  3. CIt uses one flip-flop per state with exactly one set at a time, so next-state and output logic tend to be simple and fast
Show answer and explanation

Answer: C. It uses one flip-flop per state with exactly one set at a time, so next-state and output logic tend to be simple and fast

One-hot encoding represents, say, four states as 0001, 0010, 0100 and 1000, assigning one flip-flop to each state. It needs more flip-flops, but checking whether the machine is in a state means looking at a single bit, so next-state and output logic is shallow and tends to be fast. It is common on flip-flop-rich FPGAs, while binary encoding is used when flip-flop count must be minimized.

For a state machine with three states, how many flip-flops are needed to store the state (binary encoding / one-hot encoding)?

  1. A3 / 2
  2. B2 / 3
  3. C1 / 3
Show answer and explanation

Answer: B. 2 / 3

Binary encoding numbers the states in binary, so three states fit in 2 bits (two flip-flops): 00, 01 and 10. One-hot encoding uses one flip-flop per state, so three. Binary leaves the unused code 11, so it is safer to define its destination with a default branch, letting the machine recover if noise ever puts it there. However, synthesis tools may optimize away the logic for recovering from unreachable states, so a tool setting that builds a safe state machine may also be needed.

FAQ

How many flip-flops does one-hot encoding need?

One flip-flop per state, so a machine with N states needs N flip-flops. Binary encoding of the same machine needs only the ceiling of log2 N.

What is the difference between one-hot and binary state encoding?

One-hot uses one flip-flop per state with a single bit set, giving simple, fast decode logic at the cost of more registers. Binary encoding stores the state number in the fewest flip-flops but needs wider logic to decode each state.

Is one-hot encoding in digital design the same as one-hot encoding in machine learning?

The idea is the same: a vector with exactly one element set to 1 identifies one item from a set. In hardware it encodes the current state of a state machine or a select signal, while in machine learning it represents a categorical value as a numeric vector.

Books on HDL & Verification Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary