CodiodeCodiode
Home
Problem Solving
Skill Tracks
My Assignments
Contests
Leaderboard
Community
Settings
Codiode/Problems/Arithmetic

16-bit Leading Zero Counter Loop

MediumVerilog / SystemVerilogBuild

Floating-point normalization and integer division hardware rely on leading zero counters to determine the magnitude of an operand. Manually writing priority encoders for wide data paths using cascaded conditional statements is prone to copy paste errors and scales poorly to larger bus widths.

The module computes the number of consecutive zero bits in a 16-bit input word, starting from the most significant bit (MSB, index 15) and scanning downwards to the least significant bit (LSB, index 0). If the MSB is 1, the count is 0. If the input is entirely zeros, the count is 16.

You must implement this logic using a combinational for loop inside an always_comb or always @* block. The loop should iterate through the bits and dynamically determine the leading zero count, allowing the synthesizer to unroll it into a priority encoder structure without requiring a massive 16-branch if statement.

| Signal | Direction | Width | Description | |--------|-----------|-------|-------------| | in | input | 16 | The 16-bit input data vector | | zeros| output | 5 | Number of leading zeros; ranges from 0 to 16 |

Constraints

  • Purely combinational logic; no clock or reset ports.
  • The zeros output must be exactly 5 bits wide to represent the maximum value of 16.
  • If in is 16'h0000, zeros must evaluate to 16.
  • You must use a procedural loop (e.g., a for loop) rather than manual if chains or case statements.

Topics

Combinational LogicPriority EncoderFor Loop

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against a real testbench — you get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

Sign in to solveSee what Pro unlocks

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Related problems

  • Binary to Gray Code DatapathEasy
  • Fixed Point Multiplication with SaturationHard
  • Absolute Value with Bit GrowthMedium
  • 8 bit Basic ALU with FlagsEasy
  • 8-bit Combinational PopcountMedium
  • 8-bit Logical Barrel ShifterMedium
  • 16-bit Arithmetic Barrel ShifterHard
  • Parameterized ALU with Safe DefaultsMedium

Browse all problems · Learning tracks