Write a constraint to generate a number which has binary all 1s grouped together.
class sample;
rand bit [31:0] x;
rand int num_ones;
rand int shift_by;
constraint k{
num_ones inside {[0:31]};
shift_by inside{[0:31-num_ones]};
solve num_ones before shift_by;
}
function void post_randomize();
x= {num_ones{1'b1}}<<shift_by;
endfunction
endclass
module tb;
sample s;
initial begin
s = new();
repeat(20)begin
s.randomize();
$display("%b",s.x);
end
end
endmodule
Is my solution correct for this problem.