Home > Uncategorized > Power Operator ** in Verilog ( Especially 2 to Power N)

Power Operator ** in Verilog ( Especially 2 to Power N)

The 2001 edition of Verilog introduced power operator using **.  The x**y, for example mean x to the power of y.  The most common us of power operator will be 2 to the power N which will also be easiest to understand from synthesis perspective. This code snippet, for example compares if all bits of an N bit register are one.


...

parameter N = 4;

...

....

assign clock_out_tick   =  (reg_value == 2**N-1)  ?  l'bl  :  l'b0;

Let us say N is 4. In that case 2**N -1 will have a value of 15 or 4'b1111. This is part of a code that gives a "clock tick" or a  1 signal for 1 clock period when counter reaches its maximum value.

If  N were not a parameter we could possibly have written this code


assign clock_out   =  (reg_value == 4'b1111)  ?  l'bl  :  l'b0;

You will also find its usage in localparam declaration often as in

localparam N;
localparam ALL_BITS_1       = 2 ** N -1;

Anytime you see 2**N-1 mentally assumes that it is equivalent to making all N bits 1.

In general, the operands of the power operators can take values other than 2 and N mentioned here. For example.

4**3 evaluates to 4*4*4
3**0 evaluates to 1

Some special cases
Hopefully you do not come across these - but just in case - if you raise a negative power to 0, it will become undecided.

0 **(-1) evaluates to 1/0 or 'x

Difference with C

In the power ^ is used for power operation. So 2^3 will mean 23. In verilog the ^ was taken up for Exclusive OR and hence the FORTRAN style ** was used for power operator. The Section 11.4.3 of Verilog reference manual describes the power operator.

More verilog tutorial here

Uncategorized

  1. hlide
    February 12th, 2016 at 04:20 | #1

    As far as I know, in C as in C++, operator ^ is *ALWAYS* for Exclusive OR. That is the main reason why Verilog is using operator ^ the same way as C/C++ instead of power operation.

  1. No trackbacks yet.