# Vector bit slicing with changing range

**URL:** <https://verificationacademy.com/forums/t/vector-bit-slicing-with-changing-range/37929>\
**Category:** SystemVerilog\
**Tags:** bitslice, non-constant-expression, SystemVerilog\
**Created:** [August 10, 2020, 5:17am UTC](https://verificationacademy.com/forums/t/vector-bit-slicing-with-changing-range/37929 "2020-08-10T05:17:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelP](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@MichaelP](https://verificationacademy.com/forums/u/MichaelP)\
**Post date:** [August 10, 2020, 5:17am UTC](https://verificationacademy.com/forums/t/vector-bit-slicing-with-changing-range/37929/1 "2020-08-10T05:17:39Z")

</div>

Hi,

I tried to do assignment of range of bits from one vector to another.  
The range might differ, based on another argument to the function called rd\_size.  
Received next error:  
\*Error-[NCE] Non-constant expression  
The following expression should be a constant.  
Expression: \_width  
Source info: masked\_data[orig\_addr[3:0]\*4 +: \_width] = \_temp[orig\_addr[3:0]_4 +: \_width];_

What elegant way one might use to code it, except moving the last statement of the function into each line of the case?

```verilog

function bit [127:0] masked_data(input bit[127:0] data_line, input bit[33-1:0] orig_addr, input svt_axi_transaction::burst_size_enum rd_size);
    bit[127:0] _temp = data_line;
    bit[7:0] _width;

    case (rd_size) 
        svt_axi_transaction::BURST_SIZE_8BIT : _width = 8;
        svt_axi_transaction::BURST_SIZE_16BIT : _width = 16;
        svt_axi_transaction::BURST_SIZE_32BIT : _width = 32;
        svt_axi_transaction::BURST_SIZE_64BIT : _width = 64;
        svt_axi_transaction::BURST_SIZE_128BIT : _width = 128;
        default : `uvm_fatal(get_name(), "Illegal AXI burst_size!")
    endcase
    
    masked_data[orig_addr[3:0]*4 +: _width] = _temp[orig_addr[3:0]*4 +: _width];
endfunction : masked_data

```

Thanks!

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 10, 2020, 5:16pm UTC](https://verificationacademy.com/forums/t/vector-bit-slicing-with-changing-range/37929/2 "2020-08-10T17:16:27Z")

</div>

_In reply to [Michael54](https://verificationacademy.com/forums/t/vector-bit-slicing-with-changing-range/37929):_

You can use a for loop iterating over the number of bytes to transfer. (if you encoded rd\_size correctly, it should be n\_bytes = 2\*\*rd\_size

```verilog
for(int i=0;i<n_bytes;i++) 
   masked_data[i+orig_addr[3:0]*4 +: 8] = data_line[i+orig_addr[3:0]*4 +: 8];

```
