Today we are going to see how to generate the two power values.
Unique character of two power values:
Every two power value contains only one bit is one at a time.
Ex:
consider 4 bit values.
0=0001
2=0010
4=0100
8=1000
Method 1:
First we have to find the number of ones in a given number. if number of one is equal to one that number must be a two power value.
Kindly refer my fist blog for counting the number of ones. if (cnt==1) then we can say the generated number is a two power value.
Method 2:
By using left shift operation we can produce two power values.
module TB;
bit[3:0] x;
int data[*];
initial begin
x=$urandom;
for(int i=0;i<x;i++) begin
data[i]=1<<i;
$display("Two power values %0d %0d ",x,data[i]);
end
end
endmodule
Here x will be randomized based on x loop will iterate. "i" will be the power of two. Let consider 2**5.
Binary form of one =00001=> shift the one to left direction for five times. Result is 10000. Convert into decimal.Hence the output will be 32. This is the logic behind this code.
Method 3:
By writing constraint.
class two_power;
randc bit [3:0] data;
constraint genration{(data&(data-1))==0};} //It will generate 0,2,4,8 vlaues
constraint genration{(data!=0)->(data&(data-1))==0};} //It will generate 2,4,8 vlaues
endclass
Constraint explanation:
8&(8-1) will produce a result as zero. So 8 is a two power value.
3&(3-1) will produce a result as not equal to zero so 3 is not a two power value.
Method 4:
It's a normal syntax (2**x).
module TB;
bit[3:0] x;
int y;
initial begin
x=$urandom;
for(int i=0;i<x;i++) begin
y=2**i;
$display("Two power values %0d %0d ",i,y);
end
end
endmodule
Method 5:
By using system function($pow(x,y)) we can generate two power values. By using this system function we can find any power values.
module TB;
bit[3:0] x;
int y;
initial begin
x=$urandom;
for(int i=0;i<x;i++) begin
y=$pow(2,i);
$display("Two power values %0d %0d ",i,y);
end
end
endmodule
Hope this concepts will be useful to everyone!!!
If you found any mistakes kindly comment it!!!!
Giving feedback is more precious than writing Article !!!!
Always welcome positive and negative feedback!!!😊😊😊😊
Good examples.
ReplyDeleteThanks for sharing the knowledge
It seems there is a typo in for loop condition. Kindly do change!
ReplyDeleteYes its a typo.Changes are done in blog. Thanks for the feedback
DeleteSo many solutions.Very useful
ReplyDelete