SystemVerilog #62 · 2026-11-24 · Generate and $clog2 · Easy · about 15 min
Reverse and count bits at any width
Write bit_rev_cnt, a parameterized combinational module with width W (any W >= 1, not only powers of two). It has two outputs for the W-bit input a:
rev:awith its bit order reversed, sorev[i] = a[W-1-i].cnt: the number of1bits ina. Its width is$clog2(W+1)bits, which is already declared in the port list.
Use a generate loop (or any other legal construct) so that one description works for every W.
Examples with W = 8: a = 8'b0000_0001 gives rev = 8'b1000_0000, cnt = 1. a = 8'b1101_0000 gives rev = 8'b0000_1011, cnt = 3. With W = 5, a = 5'b00110 gives rev = 5'b01100, cnt = 2. With W = 1, both outputs equal a.
Fill in bit_rev_cnt.sv. Run tries a few values at W = 8 and W = 5.
Write and run your answer in the browser. The hidden tests check it, and it’s free.