# Array Reduction methods

SystemVerilog Array Reduction methods operate on an unpacked array to reduce the array to a single value.

built-in array reduction methods are,

Method Description
sum() returns the sum of all the array elements
product() returns the product of all the array elements
and() returns the bit-wise AND ( & ) of all the array elements
or() returns the bit-wise OR ( | ) of all the array elements
xor() returns the logical XOR ( ^ ) of all the array elements

‘with’ clause is allowed for sort and rsort methods.

expression specified in “with” clause will be evaluated for each array element and performs the operation on an array.

## Array reduction methods SUM and PRODUCT

On calling sum() method sum of array_1 elements (1,2,3,4) will be returned to variable t_sum.
On calling product() method product of array_1 elements (1,2,3,4) will be returned to variable t_product.

```module fixedsize_array;

//declaration of array’s
int array_1;

int t_sum,t_product;

initial begin
//array initialization
array_1  = '{1,2,3,4};

t_sum = array_1.sum(); //t_sum = 1+2+3+4
\$display("Sum of array_1 is \t%0d",t_sum);

t_product = array_1.product(); //t_product = 1*2*3*4
\$display("product of array_1 is \t%0d",t_product);
end

endmodule
```

Simulator Output

```Sum of array_1 is 10
product of array_1 is 24```

Click to execute on ## Array reduction method AND

On calling and() method, bit-wise and (&) will be performed on all the array elements and returned.
Let’s consider an example of an array with 2, 3 and 4 elements.

Before looking into examples, see to the Truth table for AND.

 A B Y = A & B 0 0 0 0 1 0 1 0 0 1 1 1

1. Array with 2 elements.
Consider A=2 and B=3.
Y = A & B;

 Binary Decimal A 0010 2 B 0011 3 Y = A & B 0010 2

Consider A=2 and B=1.
Y = A & B;

 Binary Decimal A 0010 2 B 0001 1 Y = A & B 0000 0

2. Array with 3 elements.
Consider A=10, B=9, and C=8.

AND operation of 3 elements performed in 2 steps, In first step A & B, will be performed. In the second step result of the first step & C will be done. Considering X as the first step result.

X = A & B;
Y = X & C;

 Binary Decimal A 1010 10 B 1001 9 X = A & B 1000 8 C 1000 8 Y = X & C 1000 8

2. Array with 4 elements.
Consider A=3, B=5, C=7, and D=9.

Consider X and Y as intermediate results.
X = A & B;
Y = X & C;
Z = Y & D;

 Binary Decimal A 0011 3 B 0101 5 X = A & B 0001 8 C 0111 7 Y = X & C 0001 8 D 1001 9 Z = Y & D 0001 1

Complete code with above examples.

```module fixedsize_array;

//declaration of array’s
int array_1;
int array_2;
int array_3;
int array_4;

int b_and;

initial begin
//array initialization
array_1  = '{2,3};
array_2  = '{2,1};
array_3  = '{10,9,8};
array_4  = '{3,5,7,9};

b_and = array_1.and();
\$display("Bit-wise AND of array_1 is \t%0d",b_and);

b_and = array_2.and();
\$display("Bit-wise AND of array_2 is \t%0d",b_and);

b_and = array_3.and();
\$display("Bit-wise AND of array_3 is \t%0d",b_and);

b_and = array_4.and();
\$display("Bit-wise AND of array_4 is \t%0d",b_and);
end
endmodule
```

Simulator Output

```Bit-wise AND of array_1 is 2
Bit-wise AND of array_2 is 0
Bit-wise AND of array_3 is 8
Bit-wise AND of array_4 is 1```

Click to execute on ## Array reduction method OR

On calling or() method, bit-wise or (|) will be performed on all the array elements and returned.
Let’s consider an example of an array with 2, 3 and 4 elements.

Before looking into examples, see to the Truth table for OR.

 A B Y = A | B 0 0 0 0 1 1 1 0 1 1 1 1

1. Array with 2 elements.
Consider A=2 and B=3.
Y = A | B;

 Binary Decimal A 0010 2 B 0011 3 Y = A | B 0011 3

Consider A=2 and B=1.
Y = A | B;

 Binary Decimal A 0010 2 B 0001 1 Y = A | B 0011 3

2. Array with 3 elements.
Consider A=10, B=9, and C=8.

OR operation of 3 elements performed in 2 steps, In first step A | B will be performed. In the second step result of the first step | C will be done. Considering X as the first step result.

X = A | B;
Y = X | C;

 Binary Decimal A 1010 10 B 1001 9 X = A | B 1011 11 C 1000 8 Y = X | C 1011 11

2. Array with 4 elements.
Consider A=3, B=5, C=7, and D=9.

Consider X and Y as intermediate results.
X = A | B;
Y = X | C;
Z = Y | D;

 Binary Decimal A 0011 3 B 0101 5 X = A | B 0111 7 C 0111 7 Y = X | C 0111 7 D 1001 9 Z = Y | D 1111 15

Complete code with above examples.

```module fixedsize_array;

//declaration of array’s
int array_1;
int array_2;
int array_3;
int array_4;

int b_or;

initial begin
//array initialization
array_1  = '{2,3};
array_2  = '{2,1};
array_3  = '{10,9,8};
array_4  = '{3,5,7,9};

b_or = array_1.or();
\$display("bit-wise OR of array_1 is \t%0d",b_or);

b_or = array_2.or();
\$display("bit-wise OR of array_2 is \t%0d",b_or);

b_or = array_3.or();
\$display("bit-wise OR of array_3 is \t%0d",b_or);

b_or = array_4.or();
\$display("bit-wise OR of array_4 is \t%0d",b_or);
end

endmodule
```

Simulator Output

```bit-wise OR of array_1 is 3
bit-wise OR of array_2 is 3
bit-wise OR of array_3 is 11
bit-wise OR of array_4 is 15```

Click to execute on 