If A Number is An Exact Power of 2?

This algorithm is very simple to write. But it may be little bit complex to understand. Number is substracted by 1 & AND'ed with itself. Result should be zero for number to be power of two. This is implemented as 32 bit full adder and comparator. Following is the code
snippet for this algorithm.


 Cute & Nifty Implementation

     wire [31:0] inb_1 = inb – 1;
     always @(posedge clk or negedge rst_n) begin
       if (~rst_n)
          val <= 1'b0;
          val <= (inb & inb_1) == 0;



If implemented in ANSI-C, it will require two operations only. This is most efficient of all algorithms in software. But is it the most efficient in Hardware? Lets take a look at HW performance numbers

  • Code Size – 14 (Less the better)
  • Complexity – 7/10 on simplicity scale. (More the better – Little Subjective)
  • Basic Gate Count & Timing –  Area is 1653 micron2 (Less the better), Critical Path Delay 7.88ns(Less the better)
  • Optimizable – Area is 3509 micron2(Less the better), Critical Path Delay 1.48ns (Less the better)

Next: Design Implementation 4 – High Peformance but tricky

Leave a Reply

Your email address will not be published. Required fields are marked *