Efficient Way for EVEN or ODD
Hi everyone,
Thank you for your support! In our constraint articles, I’ve discussed an efficient way to determine whether a number is even or odd. Understanding this concept can greatly help when writing constraints that involve generating even, odd, or numbers divisible by specific values like 4 or 8.
Let’s break it down:
- Even Number: A number divisible by 2. The LSB (Least Significant Bit) of any even number is always 0. This is because adding 1 to an even number makes it odd.
- Odd Number: A number that is not divisible by 2. Adding 1 to an odd number makes it even.
- Divisibility by 4: Numbers divisible by 4 have the LSB’s two bits as 00.
if you closely the LSB bit of Any Even number that sould be always 0 why?
because by making the LSB as 1 we are making that EVEN number as an Add number by adding one to it.
0 ==> 4’b0000
1 ==> 4’b0001 ==> adding one 1 to the Even no. 0 we made is as 1
2 ==> 4’b0010
3 ==> 4’b0011 ==> again adding 1 to the Even no. 2 we made it an Odd no.
To write a constraint for generating even numbers, you can simply check that the LSB is 0: a[0] == 0.
Similary if you see all the no. Divisable by 4 will have the LSB 2 bit as 00.
4 ==> 4’b0100 8 ==> 4’b1000 12 ==> 4’b1100 16 ==> 5’b10000;
For numbers divisible by 4, you can check that the two LSBs are 00: a[1:0] == 2’b0.
Now, why is this method considered efficient? Unlike using the modulus operator % which involves division, our method involves simple bit checking. The division is essentially repetitive subtraction and can take several clock cycles to complete. On the other hand, bit checking for even/odd or divisibility by powers of 2 can be done in a single clock cycle, making it a more efficient approach.
Understanding these concepts can make your constraint writing more efficient and precise.
I appreciate your understanding of the methodology we’ve utilized to handle constraints and their real-world applications. Your support, through likes and reposts, will indeed encourage further exploration. I’m eagerly looking forward to engaging in meaningful discussions with you.
Thank you! 😊🌟