Tutorials OPERATORS PART 1

Operators : Concatenation Arithmetic Operators Relational Operators Equality Operators Logical Operators Bit-wise Operators Reduction Operators Shift Operators Increment and Decrement Operators Conditional Operator

Concatenation :

{} concatenation right of assignment.
Â´{} concatenation left of assignment.

EXAMPLE :Concatenation
program main {
bit [4:0] a;
bit b,c,d;
b = 0;
c = 1;
d = 1;
a = {b,c,0,0,d};
`{b,c,d} = 3'b111;
printf(" a %b b %b c %b d %b ",a,b,c,d);
}

RESULTS

a 00001 b 1 c 1 d 1

Arithmetic :

# - unary arithmetic (negative)
# +, -, *, / binary arithmetic
# % modulus

EXAMPLE :Arithmetic
program main {
integer a,b;
b = 10;
a = 22;

printf(" -(nagetion) is %0d \n",-(a) );
printf(" a + b is %0d \n",a+b);
printf(" a - b is %0d \n",a-b);
printf(" a * b is %0d \n",a*b);
printf(" a / b is %0d \n",a/b);
printf(" a modulus b is %0d \n",a%b);

}

RESULTS

-(nagetion) is -22
a + b is 32
a - b is 12
a * b is 220
a / b is 2
a modules b is 2

Relational :

# > >= < <= relational

EXAMPLE :Relational
program main {
integer a,b;
b = 10;
a = 22;

printf(" a < b is %0d \n",a < b);
printf(" a > b is %0d \n",a >b);
printf(" a <= b is %0d \n",a <= b);
printf(" a >= b is %0d \n",a >= b);

}
RESULTS

a < b is 0
a > b is 1
a <= b is 0
a >= b is 1

Equality : == logical equality != logical inequality === case equality !== case inequality =?= wildcard equality !?= wildcard inequality

EXAMPLE :logical Equality
reg[3:0] a;
reg[7:0] x, y, z;
a = 4'b0101;
x = 8'b1000_0101;
y = 8'b0000_0101;
z = 8'b0xx0_0101;
if (x == a)
printf("x equals a is TRUE.\n");
if (y == a)
printf("y equals a is TRUE.\n");
if (z == a)
printf("z equals a is TRUE.\n");
RESULTS

y equals a is TRUE.

EXAMPLE : Case equality
program main {
reg a_1,a_0,a_x,a_z;
reg b_1,b_0,b_x,b_z;
a_1 = 'b1;a_0 = 'b0;a_x = 'bx;a_z = 'bz;
b_1 = 'b1;b_0 = 'b0;b_x = 'bx;b_z = 'bz;
printf("--------------------------\n");
printf (" == 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 == b_0,a_0 == b_1,a_0 == b_x,a_0 == b_z);
printf (" 1 %b %b %b %b \n",a_1 == b_0,a_1 == b_1,a_1 == b_x,a_1 == b_z);
printf (" x %b %b %b %b \n",a_x == b_0,a_x == b_1,a_x == b_x,a_x == b_z);
printf (" z %b %b %b %b \n",a_z == b_0,a_z == b_1,a_z == b_x,a_z == b_z);
printf("--------------------------\n");
printf("--------------------------\n");
printf (" === 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 === b_0,a_0 === b_1,a_0 === b_x,a_0 === b_z);
printf (" 1 %b %b %b %b \n",a_1 === b_0,a_1 === b_1,a_1 === b_x,a_1 === b_z);
printf (" x %b %b %b %b \n",a_x === b_0,a_x === b_1,a_x === b_x,a_x === b_z);
printf (" z %b %b %b %b \n",a_z === b_0,a_z === b_1,a_z === b_x,a_z === b_z);
printf("--------------------------\n");
printf("--------------------------\n");
printf (" =?= 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 =?= b_0,a_0 =?= b_1,a_0 =?= b_x,a_0 =?= b_z);
printf (" 1 %b %b %b %b \n",a_1 =?= b_0,a_1 =?= b_1,a_1 =?= b_x,a_1 =?= b_z);
printf (" x %b %b %b %b \n",a_x =?= b_0,a_x =?= b_1,a_x =?= b_x,a_x =?= b_z);
printf (" z %b %b %b %b \n",a_z =?= b_0,a_z =?= b_1,a_z =?= b_x,a_z =?= b_z);
printf("--------------------------\n");
printf("--------------------------\n");
printf (" != 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 != b_0,a_0 != b_1,a_0 != b_x,a_0 != b_z);
printf (" 1 %b %b %b %b \n",a_1 != b_0,a_1 != b_1,a_1 != b_x,a_1 != b_z);
printf (" x %b %b %b %b \n",a_x != b_0,a_x != b_1,a_x != b_x,a_x != b_z);
printf (" z %b %b %b %b \n",a_z != b_0,a_z != b_1,a_z != b_x,a_z != b_z);
printf("--------------------------\n");
printf("--------------------------\n");
printf (" !== 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 !== b_0,a_0 !== b_1,a_0 !== b_x,a_0 !== b_z);
printf (" 1 %b %b %b %b \n",a_1 !== b_0,a_1 !== b_1,a_1 !== b_x,a_1 !== b_z);
printf (" x %b %b %b %b \n",a_x !== b_0,a_x !== b_1,a_x !== b_x,a_x !== b_z);
printf (" z %b %b %b %b \n",a_z !== b_0,a_z !== b_1,a_z !== b_x,a_z !== b_z);
printf("--------------------------\n");
printf("--------------------------\n");
printf (" !?= 0 1 x z \n");
printf("--------------------------\n");
printf (" 0 %b %b %b %b \n",a_0 !?= b_0,a_0 !?= b_1,a_0 !?= b_x,a_0 !?= b_z);
printf (" 1 %b %b %b %b \n",a_1 !?= b_0,a_1 !?= b_1,a_1 !?= b_x,a_1 !?= b_z);
printf (" x %b %b %b %b \n",a_x !?= b_0,a_x !?= b_1,a_x !?= b_x,a_x !?= b_z);
printf (" z %b %b %b %b \n",a_z !?= b_0,a_z !?= b_1,a_z !?= b_x,a_z !?= b_z);
printf("--------------------------\n");
}

RESULTS

--------------------------
== 0 1 x z
--------------------------
0 1 0 x x
1 0 1 x x
x x x x x
z x x x x
--------------------------
--------------------------
=== 0 1 x z
--------------------------
0 1 0 0 0
1 0 1 0 0
x 0 0 1 0
z 0 0 0 1
--------------------------
--------------------------
=?= 0 1 x z
--------------------------
0 1 0 1 1
1 0 1 1 1
x 1 1 1 1
z 1 1 1 1
--------------------------
--------------------------
!= 0 1 x z
--------------------------
0 0 1 x x
1 1 0 x x
x x x x x
z x x x x
--------------------------
--------------------------
!== 0 1 x z
--------------------------
0 0 1 1 1
1 1 0 1 1
x 1 1 0 1
z 1 1 1 0
--------------------------
--------------------------
!?= 0 1 x z
--------------------------
0 0 1 0 0
1 1 0 0 0
x 0 0 0 0
z 0 0 0 0
--------------------------

 << PREVIOUS PAGE TOP NEXT PAGE >> 