' << ' operator in verilog

前端 未结 3 1365
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-09 07:16

i have a verilog code in which there is a line as follows:

parameter ADDR_WIDTH = 8 ;
parameter RAM_DEPTH = 1 << ADDR_WIDTH;

here what wi

相关标签:
3条回答
  • 2021-02-09 07:45

    1 << ADDR_WIDTH means 1 will be shifted 8 bits to the left and will be assigned as the value for RAM_DEPTH.

    In addition, 1 << ADDR_WIDTH also means 2^ADDR_WIDTH.

    Given ADDR_WIDTH = 8, then 2^8 = 256 and that will be the value for RAM_DEPTH

    0 讨论(0)
  • 2021-02-09 07:48

    << is a binary shift, shifting 1 to the left 8 places.

    4'b0001 << 1 => 4'b0010
    

    >> is a binary right shift adding 0's to the MSB.
    >>> is a signed shift which maintains the value of the MSB if the left input is signed.

    4'sb1011 >>  1 => 0101
    4'sb1011 >>> 1 => 1101
    

    Three ways to indicate left operand is signed:

    module shift;
      logic        [3:0] test1 = 4'b1000;
      logic signed [3:0] test2 = 4'b1000;
    
      initial begin
        $display("%b", $signed(test1) >>> 1 ); //Explicitly set as signed
        $display("%b", test2          >>> 1 ); //Declared as signed type
        $display("%b", 4'sb1000       >>> 1 ); //Signed constant
        $finish;
      end
    endmodule
    
    0 讨论(0)
  • 2021-02-09 07:51

    << is the left-shift operator, as it is in many other languages.

    Here RAM_DEPTH will be 1 left-shifted by 8 bits, which is equivalent to 2^8, or 256.

    0 讨论(0)
提交回复
热议问题