module battery_led_bar (
input [3:0] level, // 0..8 inclusive
output [7:0] led_bar
);
// Build a "lowest-N-bits-one" mask:
// For level=0: ~(8'hFF << 0) = ~8'hFF = 8'h00
// For level=8: ~(8'hFF << 8) = ~8'h00 = 8'hFF
// For level=N: ~(8'hFF << N) = 00..011..1 (N ones at LSBs)
assign led_bar = ~(8'hFF << level);
endmodule
💡Remember
- Shifts + mask trick:
~(8'hFF << level) yields the lowest level bits = 1 (works for 0..8). - Sized literals help:
8'hFF, 4'd8 avoid width surprises. - Logical shift (
<<) inserts zeros; shifting by the vector width (8) produces zero—exactly what we want before the ~.