Tutorials

OPERATORS PART 2

Logical :

# ! logical negation
# && logical and
# || logical or

EXAMPLE : Logical
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 (" %b %b %b %b \n",!b_0,!b_1,!b_x,!b_z);
printf("--------------------------\n");
}
RESULTS

--------------------------
&& 0 1 x z
--------------------------
0 0 0 0 0
1 0 1 0 1
x 0 0 0 0
z 0 1 0 1
--------------------------
--------------------------
|| 0 1 x z
--------------------------
0 0 1 0 1
1 1 1 1 1
x 0 1 0 1
z 1 1 1 1
--------------------------
--------------------------
! 0 1 x z
--------------------------
1 0 x x
--------------------------

Bitwise :

# ~ bitwise negation (unary)
# & bitwise and (binary)
# &~ bitwise nand (binary)
# | bitwise or (binary)
# |~ bitwise nor (binary)
# ^ bitwise exclusive or (binary)
# ^~ bitwise exclusive nor (binary)

EXAMPLE : Bitwise
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 (" %b %b %b %b \n",~b_0,~b_1,~b_x,~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 (" |~ 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 (" ^~ 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
--------------------------
1 0 x x
--------------------------
--------------------------
& 0 1 x z
--------------------------
0 0 0 0 0
1 0 1 x x
x 0 x x x
z 0 x x x
--------------------------
--------------------------
&~ 0 1 x z
--------------------------
0 1 1 1 1
1 1 0 x x
x 1 x x x
z 1 x x x
--------------------------
--------------------------
| 0 1 x z
--------------------------
0 0 1 x x
1 1 1 1 1
x x 1 x x
z x 1 x x
--------------------------
|~ 0 1 x z
--------------------------
0 1 0 x x
1 0 0 0 0
x x 0 x x
z x 0 x x
--------------------------
--------------------------
^ 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 1 0 x x
1 0 1 x x
x x x x x
z x x x x
--------------------------

Reduction :

# & unary and
# ~& unary nand
# | unary or
# ~| unary nor
# ^ unary exclusive or
# ~^ unary exclusive nor

EXAMPLE : Reduction
program main {
reg [3:0] a_1,a_0,a_01xz,a_1xz,a_0xz,a_0dd1,a_even1;
a_1 = 4'b1111 ;
a_0 = 4'b0000 ;
a_01xz = 4'b01xz ;
a_1xz = 4'b11xz ;
a_0xz = 4'b00xz ;
a_0dd1 = 4'b1110 ;
a_even1 = 4'b1100 ;

printf("-------------------------------------------\n");
printf(" a_1 a_0 a_01xz a_1xz a_0xz \n");
printf("-------------------------------------------\n");
printf("& %b %b %b %b %b \n",&a_1,&a_0,&a_01xz,&a_1xz,&a_0xz);
printf("| %b %b %b %b %b \n",|a_1,|a_0,|a_01xz,|a_1xz,|a_0xz);
printf("~& %b %b %b %b %b \n",~&a_1,~&a_0,~&a_01xz,~&a_1xz,~&a_0xz);
printf("~| %b %b %b %b %b \n",~|a_1,~|a_0,~|a_01xz,~|a_1xz,~|a_0xz);
printf("-------------------------------------------\n");
printf(" a_ood1 a_even1 a_1xz\n");
printf("-------------------------------------------\n");
printf(" ^ %b %b %b \n",^a_0dd1,^a_even1,^a_1xz);
printf(" ~^ %b %b %b \n",~^a_0dd1,~^a_even1,~^a_1xz);
printf("-------------------------------------------\n");

}
RESULTS

-------------------------------------------
a_1 a_0 a_01xz a_1xz a_0xz
-------------------------------------------
& 1 0 0 x 0
| 1 0 1 1 x
~& 0 1 1 x 1
~| 0 1 0 0 x
-------------------------------------------
a_ood1 a_even1 a_1xz
-------------------------------------------
^ 1 0 x
~^ 0 1 x
-------------------------------------------

 << PREVIOUS PAGE TOP NEXT PAGE >>